How to create a shortcut file with .NET

Unfortunately .NET doesn't have direct support for this task but luckily you can leverage Interop and the Windows Scripting Host Object Model to do this with only a few lines of code. Windows Scripting Host is an automation technology for Microsoft Windows operating systems that provides scripting capabilities comparable to batch files, but with a greater range of supported features.


Steps to get this working:

1) Add a reference to "Windows Script Host Object Model" to your project - this will be found under the COM tab in the add reference dialog.

2) Add a using to the class where your shortcut create code will go. e.g.

using IWshRuntimeLibrary;

3) Use the following code to create a shortcut. I would recommend making a helper class for this. e.g.

    WshShell shell = new WshShell();
    IWshShortcut link = (IWshShortcut)shell.CreateShortcut("c:\My Notepad Shortcut.lnk");
    link.TargetPath = "c:\windows\notepad.exe";
    link.Save();

No comments:

Post a Comment