How to create start menu shortcut

前端 未结 3 1082
有刺的猬
有刺的猬 2021-02-04 08:41

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          


        
3条回答
  •  自闭症患者
    2021-02-04 09:24

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

提交回复
热议问题