How to register a global hotkey for your application in C#

How to register a global hotkey for your application in C#

Register a hotkey for your application that can be triggered even if your form is minimized and does not have focus. More »

How to download a file in C# (progressbar and download speed)

How to download a file in C# (progressbar and download speed)

Learn how to download files in C# while displaying the percentage and the download speed. More »

UDP hole punching implementation in C#

UDP hole punching implementation in C#

Learn how to implement UDP hole punching so you can make your clients life a lot easier by not forcing them to open ports on their end. More »

 

Create shortcut programmatically in C#

In cases where we need to create a shortcut for our application or for any other reason, the Windows Script Host Object Model library allows us to do just that.

In order to access the classes that will enable us to create shortcuts we need to add the Windows Script Host Object Model library as a reference to our project first.

  1. Right click on your project
  2. Click “Add Reference…”
  3. Select the “COM” tab on the left
  4. Search for Windows Script Host Object Model and add it as a reference

Windows Script Host Object Model add reference

After you successfully add the reference in your project you should be able to use the snippet bellow to create shortcuts as you please.

Create shortcut:

Usage:

Share on FacebookTweet about this on TwitterShare on Google+Share on StumbleUponShare on LinkedInShare on RedditPin on PinterestShare on TumblrDigg thisPrint this pageEmail this to someone

4 Responses to Create shortcut programmatically in C#

  1. TrungNV says:

    erro System.IO.Path.Combine(shortcutPath, shortcutName + “.lnk”);

    could not found

  2. Also set property working directory(as below)
    shortcut.WorkingDirectory = rootPath;

    Otherwise the relative path used in application will get failed

  3. Vixremento says:

    This works!

    Sadly though it only seems to create it for the current user…the moment I switch users it does not display the shortcut :(

    • CooLMinE says:

      That is where Environment.SpecialFolder.Desktop points to. If you want to target all users desktop then you will need to use Environment.SpecialFolder.CommonDesktopDirectory (requires .NET 4 or higher).

      If you are using an older version of the .NET framework you will probably need to find another method if retrieving the desktop path, probably with the use of SHGetFolderPath.

Leave a Reply

Your email address will not be published. Required fields are marked *