Skip navigation

Daily Archives: March 1st, 2007

Author: Jeff Noble

I found that it was difficult to find information on creating a desktop shortcut via code. I was able to find a solution that fit my needs. Lets face it, my needs rule ;) .

The shortcut I needed was to launch a ClickOnce application which uses a url to launch. Here is what I was able to come up with. Some of the code was found online (I don’t remember the source, but if I can find it again, I’ll give credit).


 IWshShortcut link = (IWshShortcut)shell.CreateShortcut(
  Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
  + @"\" + "LinkName.lnk");
  link.TargetPath = @"\\jnoble\sharelocation\ClickOnce.application";
  link.IconLocation = @"\\jnoble\sharelocation\icon.ico";
  link.Save();

Lets examine the code:

  1. First we get an IWshShortcut object called link. It points to the desktop location. The LinkName.lnk refers to a name you give your shortcut.
  2. The targetPath property sets the executable to launch when the shortcut is clicked. In the case of a ClickOnce application, you refer to the manifest file which ends in .application.
  3. I have the desktop shortcut icon living in the ClickOnce file share, so I refer to it there. You can also distribute the icon and refer to it in a path if you like.
  4. Last, just save the shortcut. This will write it to the desktop (in this case).

That’s it!

Enjoy

-Savij

Follow

Get every new post delivered to your Inbox.