Create shortcut of the executable programmatically using c#

The following code snippet demonstrates that how can you create a short cut of any windows application executable programmitically using C#.

Add reference of the ‘Windows Script Host Object’

Before using the above code you need to add reference of ‘Windows Script Host Object’ from the ‘com’ tab.

To add the reference of Windows Script Host Object, you can right click on the References menu item in the Solution Explorer of your project. Select the Add Reference menu. From the Add Reference dialog, click the .COM tab and select the ‘Windows Scripting Host Object Model’ and click the ‘Add’ button.

Add namespace ‘IWshRuntimeLibrary’ with ‘using’ directive in your code.

using IWshRuntimeLibrary;

Write the code

 private void CreateShortcut(string path, string targetpath, string shortcutname)
       var wsh = new IWshShell_Class();
       IWshRuntimeLibrary.IWshShortcut shortcut = wsh.CreateShortcut(
         path + "\\" + shortcutname + ".lnk") as IWshRuntimeLibrary.IWshShortcut;
       shortcut.TargetPath = targetpath;

Create shortcut

Let’s suppose you want to create the shortcut of the current windows application on the windows start up folder then you can invoke above function as:

string _path = Environment.GetFolderPath(Environment.SpecialFolder.StartMenu);
CreateShortcut(_path, Application.ExecutablePath, "myshortcut");

Please let me know if you face any problem to implement the above code.