views:

10998

answers:

5

How do you create an application shortcut (.lnk file) in C# or using the .NET framework?

The result would be a .lnk file to the specified application or URL.

+1  A: 

You could try this: http://www.geekpedia.com/tutorial125_Create-shortcuts-with-a-.NET-application.html

Kieron
Thanks. I'd seen this link and code. I was just wondering if there was a way to do it without WSH.
rathkopf
+1  A: 

Take a look at the format spec and manually write it out - it's not very complex. Way better (IMO) than most suggestions that tell you to use WSH.

ctacke
Thanks, this would be great, but this is a quick one-off for a utility for a client, so I don't think that I can invest the time required to do this properly. It would be great if it were available in the public domain somewhere.
rathkopf
This would work great until MS decides to change the format of the shortcut. If it's not documented by MS, it's an implementation detail and they're free to change it. That's why WSH exists: if the format changes, WSH will be updated.
OwenP
Dangerous/brittle, as others noted, but useful, so I upvoted.
PhiLho
Yes, it's brittle, though I'm no so certain why everyone thinks it's so bad (bad enough to downmod several times). If MS changes the format, then they have to change WSH, and that change has to be propagated to every PC out there. Exactly what is the liklihood of that?
ctacke
if they changed it the windows OS would likely still be able to read the old format, so I'm not sure why people would downvote this?
Maslow
It's the wrong with to do. Poorly written software, that go spellunking through undocumented registry keys, files, memory structures, handles and object v-tables is the reason software breaks. And it's the reason Microsoft has to maintain so many compatibility hacks and cannot innovate. Don't intentionally do the wrong thing when there's a proper API around.
Ian Boyd
+14  A: 

It's not as simple as I'd have liked, but there is a great class call ShellLink.cs at

.net vbAccelerator graphic

This code uses interop, but does not rely on WSH.

Using this class, the code to create the shortcut is:

private static void configStep_addShortcutToStartupGroup()
{
using (ShellLink shortcut = new ShellLink())
{
    shortcut.Target = Application.ExecutablePath;
    shortcut.WorkingDirectory = Path.GetDirectoryName(Application.ExecutablePath);
    shortcut.Description = "My Shorcut Name Here";
    shortcut.DisplayMode = ShellLink.LinkDisplayMode.edmNormal;
    shortcut.Save(STARTUP_SHORTCUT_FILEPATH);
}
}
rathkopf
Anyone tried ShellLink on Vista? Looks like the code was written in 2003.
blak3r
+2  A: 

I found something like this:

private void appShortcutToDesktop(string linkName)
{
    string deskDir = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);

    using (StreamWriter writer = new StreamWriter(deskDir + "\\" + linkName + ".url"))
    {
        string app = System.Reflection.Assembly.GetExecutingAssembly().Location;
        writer.WriteLine("[InternetShortcut]");
        writer.WriteLine("URL=file:///" + app);
        writer.WriteLine("IconIndex=0");
        string icon = app.Replace('\\', '/');
        writer.WriteLine("IconFile=" + icon);
        writer.Flush();
    }
}

Original code at sorrowman's article "url-link-to-desktop"

Anuraj
Oh jesus. Truly horrifying. Reversing engineering an undocumented file format, rather than using the intended API.
Ian Boyd
A: 

Is there any simpler way to do this ?
Couldn't someone create a class that had methods like createShortcut("app-path") or something !!

Phani
-1: This should be a comment.
Richard
@Richard. Users with 1 rep cannot leave comments to questions they did not ask. A discusion is here: http://meta.stackoverflow.com/questions/54329/what-can-we-do-for-new-users-who-want-to-ask-a-question-thats-already-been-asked
YWE