Posts tagged ‘c#’

Creating Windows shortcuts in .NET

If you’ve ever wanted to create a shortcut to a program in Windows, it’s not as easy as it looks.  That said, it’s also not very hard if you just use the Windows Script Host Object Model to create the shortcut:

1. Add a reference to the Windows Script Host OM:

Adding a reference to "Windows Script Host Object Model"

2. I also had to set Embed Interop Types to false:

Set "Embed Interop Types" to false

3. Create your shortcut.  The path you pass to CreateShortcut is the path to the .lnk file (e.g. c:UsersYourNameDesktopMy Fancy Shortcut.lnk)

IWshRuntimeLibrary.WshShellClass wshShell = new IWshRuntimeLibrary.WshShellClass();
IWshRuntimeLibrary.WshShortcut shortcut = wshShell.CreateShortcut(path) as IWshRuntimeLibrary.WshShortcut;

4. Modify any of the other properties on your shortcut, like setting the target, working directory, or icon.

5. Call Save() to persist your changes.

That’s all there is to it.