Read location in .lnk (shortcut) file
-
Is there any way to read the file that a .lnk (shortcut) refers to? Opening the file in Notepad shows that it is not plain text (such as Adobe Gamma Loader.lnk which loads Program Files\Common Files\Adobe\Calibration\Adobe Gamma Loader.exe). I am wondering if there is any way to read a .lnk file's location using some sort of library. Thanks, Josh
-
Is there any way to read the file that a .lnk (shortcut) refers to? Opening the file in Notepad shows that it is not plain text (such as Adobe Gamma Loader.lnk which loads Program Files\Common Files\Adobe\Calibration\Adobe Gamma Loader.exe). I am wondering if there is any way to read a .lnk file's location using some sort of library. Thanks, Josh
You can use the Windows Script Host Object Model COM library for this. If you're using Visual Studio, add a reference to it by selecting the COM tab from the Add Reference dialog and select "Windows Script Host Object Model". This will create an interop assembly called Interop.IWshRuntimeLibrary and will reference that. Then you can use the following code to extract the target file from a .lnk file...
using IWshRuntimeLibrary;
...
WshShell shell = new WshShell();
IWshShortcut link = (IWshShortcut)shell.CreateShortcut( linkPath ); //where linkPath is the filepath to the .lnk file
Process.Start( link.TargetPath ); //link.TargetPath points to the file the link targetsHope that helps :)
“Accept that some days you are the pigeon, and some days you are the statue” -- David Brent Cheers, Will
-
You can use the Windows Script Host Object Model COM library for this. If you're using Visual Studio, add a reference to it by selecting the COM tab from the Add Reference dialog and select "Windows Script Host Object Model". This will create an interop assembly called Interop.IWshRuntimeLibrary and will reference that. Then you can use the following code to extract the target file from a .lnk file...
using IWshRuntimeLibrary;
...
WshShell shell = new WshShell();
IWshShortcut link = (IWshShortcut)shell.CreateShortcut( linkPath ); //where linkPath is the filepath to the .lnk file
Process.Start( link.TargetPath ); //link.TargetPath points to the file the link targetsHope that helps :)
“Accept that some days you are the pigeon, and some days you are the statue” -- David Brent Cheers, Will
Thanks for the help Will. It works great. -Josh