Tuesday, December 12, 2006

Create .lnk file in C#

Here is some C# code to create a shortcut to a folder. These are actually .lnk files which are handled in a special way by Windows Explorer.

First include a reference to C:\Windows\System32\wshom.ocx

Second, include the following using statement :-

using IWshRuntimeLibrary;

Third, Here is the code :-


// This creates a Folder Shortcut

IWshShell wsh = new WshShellClass();
IWshShortcut shortcut = (IWshShortcut) wsh.CreateShortcut (shortcutpathfilename);

shortcut.TargetPath = targetdir;

shortcut.Save();

shortcutpathfilename is a path & filename of the .lnk file.

targetdir is the directory the link points to.


1 comment:

Evan said...

This helped me out today, thanks.