To get the target of a shortcut (.lnk
file extension) you’ll need first to have the following COM
object: Windows Script Host Object Model
Then, you may use WshShell
(or WshShellClass
) and IWshShortcut
interfaces to get the target of a shortcut
Example
string linkPathName = @"D:\Picrofo Autobot.lnk"; // Change this to the shortcut path
if (System.IO.File.Exists(linkPathName))
{
// WshShellClass shell = new WshShellClass();
WshShell shell = new WshShell(); //Create a new WshShell Interface
IWshShortcut link = (IWshShortcut)shell.CreateShortcut(linkPathName); //Link the interface to our shortcut
MessageBox.Show(link.TargetPath); //Show the target in a MessageBox using IWshShortcut
}
Thanks,
I hope you find this helpful 🙂
You may try the following steps to add Windows Script Host Object Model
to your project
- Under Solution Explorer, Right-click your project name and select Add Reference
- Select the tab COM from the pop-up Window
- Under Component Name, select Windows Script Host Object Model
- Click on OK