C# - Creating URL shortcut to desktop

These are converted from Delphi to C#, so if there are issues or problems, please do tell me.

First add code:

using System.IO;

on top of your source in using clauses.

Then create following function:

private void urlShortcutToDesktop(string linkName, string linkUrl)
{
    string deskDir = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);

    using (StreamWriter writer = new StreamWriter(deskDir + "\\" + linkName + ".url"))
    {
        writer.WriteLine("[InternetShortcut]");
        writer.WriteLine("URL=" + linkUrl);
        writer.Flush();
    }
}

Now if you call it using something like this:

urlShortcutToDesktop("google link", "http://www.google.com/");

it should create shortcut in your desktop.

C# - Creating application shortcut to desktop - similar way

You can also create shortcuts to applications similar way.

Here is code that shortcut to application and set icon for it.

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();
    }
}

Now if you call it using something like this:

appShortcutToDesktop("My App");

Ofcourse you should make some sanity checks, like if link with that name already exists, then what, etc.

Have a comment or question? send your comments or questions