Posts Tagged ‘ Shortcut ’

Create application Shortcut in .Net the easy way

Have you ever needed to create an application shortcut on the fly in your .Net application? Well I have and this is what I did. The easiest way to create a shortcut is to create a url file which contains a reference to your file instead of a web address. A url file is basically like an ini file. To create a real desktop shortcut requires the use of a COM object, which for my case was not needed.

A url file is a plain text file with the “url” file extension. Below is an Example of the required content of a url file:

There are other options that can be added to the url file as well, but all you need to specify is the URL. Rather than using a url to a internet location you can create one for a file on your system by setting URL to file:///c:/Temp/ReferenceFile.txt. Example of a url file content with additional options to specify the icon:
URL = file:///c:/Temp/ReferenceFile.txt
IconIndex = 0
IconFile = C:\Temp\application.ico

Here is an example of how to create the url file in C# using .Net 2.0:
using System;
using System.IO;
using System.Text;

public static class Program {
  public static void Main() {

  private static void CreateDesktopShortcut() {
    String appName = "Application.exe";
    String appDirectory = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
    String shortcutName = "Application";
    String shortcutPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
    StringBuilder sb = new StringBuilder();

    sb.AppendLine(String.Format(@"URL=file:///{0}/{1}", appDirectory, appName).Replace(@"\", @"/"));

    File.WriteAllText(String.Format(@"{0}\{1}.url", shortcutPath, shortcutName), sb.ToString());