Title: Get information about Windows shortcuts in C#
This post explains how to get information about Windows shortcuts. The post has two parts. The first explains how to get the information. The second explains how to create shortcuts.
Getting Shortcut Information
This example uses the following GetShortcutInfo method to get information about shortcuts.
// Get information about this link.
// Return an error message if there's a problem.
private string GetShortcutInfo(string full_name,
out string name, out string path, out string descr,
out string working_dir, out string args)
{
name = "";
path = "";
descr = "";
working_dir = "";
args = "";
try
{
// Make a Shell object.
Shell32.Shell shell = new Shell32.Shell();
// Get the shortcut's folder and name.
string shortcut_path =
full_name.Substring(0, full_name.LastIndexOf("\\"));
string shortcut_name =
full_name.Substring(full_name.LastIndexOf("\\") + 1);
if (!shortcut_name.EndsWith(".lnk"))
shortcut_name += ".lnk";
// Get the shortcut's folder.
Shell32.Folder shortcut_folder =
shell.NameSpace(shortcut_path);
// Get the shortcut's file.
Shell32.FolderItem folder_item =
shortcut_folder.Items().Item(shortcut_name);
if (folder_item == null)
return "Cannot find shortcut file '" + full_name + "'";
if (!folder_item.IsLink)
return "File '" + full_name + "' isn't a shortcut.";
// Display the shortcut's information.
Shell32.ShellLinkObject lnk =
(Shell32.ShellLinkObject)folder_item.GetLink;
name = folder_item.Name;
descr = lnk.Description;
path = lnk.Path;
working_dir = lnk.WorkingDirectory;
args = lnk.Arguments;
return "";
}
catch (Exception ex)
{
return ex.Message;
}
}
The program first creates a Shell32.Shell object to work with the Windows shell. It uses that object's NameSpace method to get a Folder object representing the folder that contains the shortcut of interest. It then uses that object's Items collection to get the shortcut's name and a ShellLinkObject representing the shortcut. From that object, the method finally gets the rest of the information it needs.
If you look closely at the picture, you'll see that in that example the shortcut's ...
- ... name was "Signature"
- ... description was "Open the file sig.txt"
- ... path was the path to the WordPad executable
- ... working directory was my desktop
- ... arguments was "sig.txt"
So when I double-click this shortcut, it starts WordPad and opens the file sig.txt on my desktop.
Making Shortcuts
I tested this in Windows 10. The details may differ slightly in other versions of Windows.
To create a shortcut, right-click on the desktop or in a folder, select New > Shortcut as shown below.
This makes Windows display the following dialog.
Enter the path to the program that you want to execute followed by any parameters that you want to pass into that program. In this example, the program is wordpad.exe and the parameter is the name of the file (sig.txt) that WordPad should edit.
Note that paths and parameters that include spaces should be surrounded by quotes as shown in the picture.
After you fill in the program and its parameters, click Next to display the following dialog.
Enter the name that you want to give the shortcut here and click Finish to create the shortcut.
To fine-tune the shortcut, right-click on it and select Properties to display the following dialog.
Here you can enter the start directory (in the Start In field) and the description (in the Comment field).
Download the example to experiment with it and to see additional details.
|