Create Desktop Shortcut using C#

 In C#, you can easily create desktop shortcut icon using Windows Script Host library. In this article, I am going write C# code examples to create desktop shortcut for Current User and All Users profile.

Summary:

Create Desktop Shortcut for Current User

The below C# method creates desktop shortcut icon to open Application settings UI. The function call of Environment.DesktopDirectory gives current user’s desktop profile path, so the created shortcuts will be available only in current user‘s Desktop location.

Note: To use Windows Script Host library, you need to add a reference under References > COM tab > Windows Script Host Object Model.

using IWshRuntimeLibrary;
-----------------------
public static void CreateDesktopShortcut()
{
  string desktopFolder = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
    WshShellClass shellClass = new WshShellClass();
    //Create Desktop Shortcut for Application Settings
    string settingsLink = Path.Combine(desktopFolder, "Settings.lnk");
    IWshShortcut shortcut = (IWshShortcut)shellClass.CreateShortcut(settingsLink);
    shortcut.TargetPath = @"C:\Program FilesMorganTechSpaceMyAppSettings.exe";
    shortcut.IconLocation = @"C:\Program FilesMorganTechSpacesettings.ico";
    shortcut.Arguments = "arg1 arg2";
    shortcut.Description = "Click to edit MorganApp settings";
    shortcut.Save();
}

Create Desktop Shortcut Icon for All Users

You can get common desktop profile path for All Users by using API function SHGetSpecialFolderPath. The below C# method creates desktop shortcut, since the function call of SHGetSpecialFolderPath returns common desktop profile path for All Users, so the created shortcuts will be available in all user‘s desktop location.

using IWshRuntimeLibrary;
using System.Runtime.InteropServices;
---------------------------------
[DllImport("shell32.dll")]
static extern bool SHGetSpecialFolderPath(IntPtr hwndOwner, [Out] StringBuilder lpszPath, int nFolder, bool fCreate);
const int CSIDL_COMMON_DESKTOPDIRECTORY = 0x19;

public static void CreateDesktopShortcutForAllUsers()
{
    StringBuilder allUserProfile = new StringBuilder(260);
    SHGetSpecialFolderPath(IntPtr.Zero, allUserProfile, CSIDL_COMMON_DESKTOPDIRECTORY, false);
    //The above API call returns: C:UsersPublicDesktop 
    string settingsLink = Path.Combine(allUserProfile.ToString(), "Settings.lnk");
    //Create All Users Desktop Shortcut for Application Settings
    WshShellClass shellClass = new WshShellClass();
    IWshShortcut shortcut = (IWshShortcut)shellClass.CreateShortcut(settingsLink);
    shortcut.TargetPath = @"C:\Program FilesMorganTechSpaceMyAppSettings.exe";
    shortcut.IconLocation = @"C:\Program FilesMorganTechSpacesettings.ico";
    shortcut.Arguments = "arg1 arg2";
    shortcut.Description = "Click to edit MorganApp settings";
    shortcut.Save();
}
Create Desktop Shortcut using C#

Delete Desktop Shortcut Icon using C#

The below C# method deletes desktop shortcuts from both current user profile and all user‘s common profile.

[DllImport("shell32.dll")]
static extern bool SHGetSpecialFolderPath(IntPtr hwndOwner, [Out] StringBuilder lpszPath, int nFolder, bool fCreate);
const int CSIDL_COMMON_DESKTOPDIRECTORY = 0x19;

public static void DeleteDesktopShortcut()
{
    //Current User: Delete current user's Desktop shortcut
  string desktopFolder = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
    string shortcut_link = Path.Combine(desktopFolder, "Settings.lnk");
    if (System.IO.File.Exists(shortcut_link))
    {
        System.IO.File.Delete(shortcut_link);
    }
    //All Users: Delete All User's Desktop shortcut
    StringBuilder allUserProfile = new StringBuilder(260);
    SHGetSpecialFolderPath(IntPtr.Zero, allUserProfile, CSIDL_COMMON_DESKTOPDIRECTORY, false);
    string common_shortcut_link = Path.Combine(allUserProfile.ToString(), "Settings.lnk");
    if (System.IO.File.Exists(common_shortcut_link))
    {
        System.IO.File.Delete(common_shortcut_link);
    }
}


Advertisement

Leave a Comment