Create Start Menu Shortcut (All Programs) using C#

By MSI setup, you can easily create Start Menu Shortcut for your application. But when it comes to custom installer setup, we need to write custom code to create All Programs shortcut. In C#, you can create shortcut using Windows Script Host library. In this article, I am going write C# code examples to create Start Menu (All Programs) shortcut for Current User and All Users profile.

Summary:

Create Start Menu Shortcut (All Programs) for Current User

The below C# method creates Start Menu >All Programs shortcuts to open Application settings UI and Uninstall setup shortcut. The function call of Environment.GetFolderPath gives current user’s profile path, so the created shortcuts will be available only in current user‘s Start Menu (All Programs) 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 CreateStartMenuShortcut()
{
    string programs_path = Environment.GetFolderPath(Environment.SpecialFolder.Programs);
    string shortcutFolder = Path.Combine(programs_path, @"MorganTechSpaceSampleApp");
    if (!Directory.Exists(shortcutFolder))
    {
        Directory.CreateDirectory(shortcutFolder);
    }
    WshShellClass shellClass = new WshShellClass();
    //Create First Shortcut for Application Settings
    string settingsLink = Path.Combine(shortcutFolder, "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 Second Shortcut for Uninstall Setup
    string uninstallLink = Path.Combine(shortcutFolder, "Uninstall.lnk");
    shortcut = (IWshShortcut)shellClass.CreateShortcut(uninstallLink);
    shortcut.TargetPath = @"C:\Program FilesMorganTechSpaceSetup.exe";
    shortcut.IconLocation = @"C:\Program FilesMorganTechSpaceuninstall.ico";
    shortcut.Arguments = "u";
    shortcut.Save();
}

Create Start Menu Shortcut (All Programs) for All Users

You can get common profile path for All Users by using API function SHGetSpecialFolderPath. The below C# method creates All Programs shortcut, since the function call of SHGetSpecialFolderPath returns common profile path for All Users, so the created shortcuts will be available in all user’s Start Menu (All Programs) 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_STARTMENU = 0x16;

public static void CreateShortcutForAllUsers()
{
    StringBuilder allUserProfile = new StringBuilder(260);
    SHGetSpecialFolderPath(IntPtr.Zero, allUserProfile, CSIDL_COMMON_STARTMENU, false);
    //The above API call returns: C:ProgramDataMicrosoftWindowsStart Menu
    string programs_path = Path.Combine(allUserProfile.ToString(), "Programs");

    //You can even use below one line code to avoid api call
    //string programs_path= Path.Combine(Environment.GetEnvironmentVariable("ALLUSERSPROFILE"), @"MicrosoftWindowsStart MenuPrograms");
    string shortcutFolder = Path.Combine(programs_path, @"MorganTechSpaceSampleApp");
    if (!Directory.Exists(shortcutFolder))
    {
        Directory.CreateDirectory(shortcutFolder);
    }
    WshShellClass shellClass = new WshShellClass();
    //Create Shortcut for Application Settings
    string settingsLink = Path.Combine(shortcutFolder, "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 Start Menu Shortcut (All Programs) using C#

Delete All Programs Shortcut using C#

The below C# method deletes start menu 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_STARTMENU = 0x16;

public static void DeleteShortcut()
{
    //Current User: Delete current user's start menu (All Programs) shortcut
    string programs_path = Environment.GetFolderPath(Environment.SpecialFolder.Programs);
    string shortcutFolder = Path.Combine(programs_path, @"MorganTechSpace");
    if (Directory.Exists(shortcutFolder))
    {
        Directory.Delete(shortcutFolder, true);
    }
    //All Users: Delete all user's start menu (All Programs) shortcut
    StringBuilder allUserProfile = new StringBuilder(260);
    SHGetSpecialFolderPath(IntPtr.Zero, allUserProfile, CSIDL_COMMON_STARTMENU, false);
    string all_users_programs_path = Path.Combine(allUserProfile.ToString(), "Programs");

    string allusershortcutFolder = Path.Combine(all_users_programs_path, @"MorganTechSpace");
    if (Directory.Exists(allusershortcutFolder))
    {
        Directory.Delete(allusershortcutFolder, true);
    }
}


Advertisement

Leave a Comment