create a desktop shortcut in csharp

To create a desktop shortcut in C#, you can use the IWshRuntimeLibrary COM library. Here's an example code that creates a shortcut for a file:

main.cs
using IWshRuntimeLibrary;

public void CreateShortcut(string filePath, string shortcutPath)
{
    // Create a new WshShellClass instance
    WshShellClass shell = new WshShellClass();

    // Create a shortcut object
    IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutPath);

    // Set shortcut properties
    shortcut.TargetPath = filePath; // The target file path
    shortcut.WorkingDirectory = ""; // The working directory of the target file
    shortcut.WindowStyle = 1; // Normal window style
    shortcut.Description = "My shortcut"; // A short description of the shortcut
    shortcut.IconLocation = "path\\to\\icon.ico"; // The path to the icon of the shortcut

    // Save the shortcut
    shortcut.Save();
}
740 chars
21 lines

To use this method, you need to pass two parameters: filePath, the path of the file for which you want to create a shortcut, and shortcutPath, the path where you want to save the shortcut (including the .lnk extension). For example:

main.cs
CreateShortcut("C:\\myFile.txt", "C:\\Users\\myUser\\Desktop\\myShortcut.lnk");
80 chars
2 lines

gistlibby LogSnag