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.

One thought on “Create shortcut of the executable programmatically using c#”

  1. Hi ,
    I get error when the target path contains the below mentioned string .
    string targetFileLocation=@”E:PackagesCác con cáo nâu nhanh chóng nhảy qua chú chó lười biếngCác con cáo nâu nhanh chóng nhảy qua chú chó lười biếngVIEWSHomePage.html”;
    I get Value does not fall within the expected range exception

Comments are closed.