Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create start menu shortcut

I am building a custom installer. How can I create a shortcut to an executable in the start menu? This is what I've come up with so far:

    string pathToExe = @"C:\Program Files (x86)\TestApp\TestApp.exe";
    string commonStartMenuPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonStartMenu);
    string appStartMenuPath = Path.Combine(commonStartMenuPath, "Programs", "TestApp");
    // TODO: create shortcut in appStartMenuPath

I'm targeting Windows 7.

like image 427
invertigo Avatar asked Jul 29 '14 20:07

invertigo


People also ask

How do I add a shortcut to the Start menu in Windows 11?

Select Start , scroll to the app you want to pin, then press and hold (or right-click) the app. Select More > Pin to taskbar. If the app is already open on the desktop, press and hold (or right click) the app's taskbar icon, and then select Pin to taskbar.

How do I add a shortcut to the Start menu for all users in Windows 10?

The easiest way to add an item to the Start menu for all users is to click the Start button then right-click on All Programs. Select the Open All Users action item, shown here. The location C:\ProgramData\Microsoft\Windows\Start Menu will open. You can create shortcuts here and they'll show up for all users.


1 Answers

Using the Windows Script Host (make sure to add a reference to the Windows Script Host Object Model, under References > COM tab):

using IWshRuntimeLibrary;

private static void AddShortcut()
{
    string pathToExe = @"C:\Program Files (x86)\TestApp\TestApp.exe";
    string commonStartMenuPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonStartMenu);
    string appStartMenuPath = Path.Combine(commonStartMenuPath, "Programs", "TestApp");

    if (!Directory.Exists(appStartMenuPath))
        Directory.CreateDirectory(appStartMenuPath);

    string shortcutLocation = Path.Combine(appStartMenuPath, "Shortcut to Test App" + ".lnk");
    WshShell shell = new WshShell();
    IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutLocation);

    shortcut.Description = "Test App Description";
    //shortcut.IconLocation = @"C:\Program Files (x86)\TestApp\TestApp.ico"; //uncomment to set the icon of the shortcut
    shortcut.TargetPath = pathToExe;
    shortcut.Save(); 
}
like image 154
raney Avatar answered Oct 02 '22 03:10

raney