How do you create an application shortcut (.lnk file) in C# or using the .NET framework?
The result would be a .lnk file to the specified application or URL.
How do you create an application shortcut (.lnk file) in C# or using the .NET framework?
The result would be a .lnk file to the specified application or URL.
Donwload IWshRuntimeLibrary
You also need to import of COM library
IWshRuntimeLibrary
. Right click on your project -> add reference -> COM -> IWshRuntimeLibrary -> add and then use the following code snippet.Similar to IllidanS4's answer, using the Windows Script Host proved the be the easiest solution for me (tested on Windows 8 64 bit).
However, rather than importing the COM type manually through code, it is easier to just add the COM type library as a reference. Choose
References->Add Reference...
,COM->Type Libraries
and find and add "Windows Script Host Object Model".This imports the namespace
IWshRuntimeLibrary
, from which you can access:Credit goes to Jim Hollenhorst.
Nice and clean. (.NET 4.0)
That's it, no additional code needed. CreateShortcut can even load shortcut from file, so properties like TargetPath return existing information. Shortcut object properties.
Also possible this way for versions of .NET unsupporting dynamic types. (.NET 3.5)
It's not as simple as I'd have liked, but there is a great class call ShellLink.cs at vbAccelerator
This code uses interop, but does not rely on WSH.
Using this class, the code to create the shortcut is:
I found something like this:
Original code at sorrowman's article "url-link-to-desktop"
After surveying all possibilities I found on SO I've settled on ShellLink:
Apart of being simple and effective, the author (Mattias Sjögren, MS MVP) is some sort of COM/PInvoke/Interop guru, and perusing his code I believe it is more robust than the alternatives.
It should be mentioned that shortcut files can also be created by several commandline utilities (which in turn can be easily invoked from C#/.NET). I never tried any of them, but I'd start with NirCmd (NirSoft have SysInternals-like quality tools).
Unfortunately NirCmd can't parse shortcut files (only create them), but for that purpose TZWorks lp seems capable. It can even format its output as csv. lnk-parser looks good too (it can output both HTML and CSV).