【问题标题】:How to create start menu shortcut如何创建开始菜单快捷方式
【发布时间】:2014-07-29 20:52:00
【问题描述】:

我正在构建一个自定义安装程序。如何在开始菜单中创建可执行文件的快捷方式?到目前为止,这是我想出的:

    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

我的目标是 Windows 7。

【问题讨论】:

    标签: c# .net-4.0 shortcut


    【解决方案1】:

    使用 Windows 脚本宿主(确保添加对 Windows 脚本宿主对象模型的引用,在 References > COM 选项卡下):

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

    【讨论】:

      【解决方案2】:

      通过 MSI 设置,您可以轻松地为您的应用程序创建 开始菜单 快捷方式。但是当涉及到自定义安装程序设置时,您需要编写自定义代码来创建所有程序快捷方式。在 C# 中,您可以使用 Windows Script Host library 创建快捷方式。

      注意:要使用Windows Script Host 库,您需要在References > COM 选项卡> Windows Script Host Object Model 下添加一个引用。

      查看这篇文章了解更多信息:http://www.morgantechspace.com/2015/01/create-start-menu-shortcut-all-programs-csharp.html

      仅为当前用户创建快捷方式

      string programs_path = Environment.GetFolderPath(Environment.SpecialFolder.Programs);
          string shortcutFolder = Path.Combine(programs_path, @"YourAppFolder\SampleApp");
          if (!Directory.Exists(shortcutFolder))
          {
              Directory.CreateDirectory(shortcutFolder);
          }
          WshShellClass shellClass = new WshShellClass();
          string settingsLink = Path.Combine(shortcutFolder, "Settings.lnk");
          IWshShortcut shortcut = (IWshShortcut)shellClass.CreateShortcut(settingsLink);
          shortcut.TargetPath = @"C:\Program Files\YourAppFolder\MyAppSettings.exe";
          shortcut.IconLocation = @"C:\Program Files\YourAppFolder\settings.ico";
          shortcut.Arguments = "arg1 arg2";
          shortcut.Description = "Click to edit SampleApp settings";
          shortcut.Save();
      

      为所有用户创建快捷方式

      您可以使用 API 函数 SHGetSpecialFolderPath 获取所有用户的通用配置文件路径。

      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:\ProgramData\Microsoft\Windows\Start Menu
          string programs_path = Path.Combine(allUserProfile.ToString(), "Programs");
      
          string shortcutFolder = Path.Combine(programs_path, @"YourAppFolder\SampleApp");
          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 Files\YourAppFolder\MyAppSettings.exe";
          shortcut.IconLocation = @"C:\Program Files\YourAppFolder\settings.ico";
          shortcut.Arguments = "arg1 arg2";
          shortcut.Description = "Click to edit SampleApp settings";
          shortcut.Save();
      }
      

      【讨论】:

        【解决方案3】:

        这与这个问题几乎相同:Create shortcut on desktop C#

        要从 answer 复制,您需要自己创建快捷方式文件。

        using (StreamWriter writer = new StreamWriter(appStartMenuPath + ".url"))
        {
            writer.WriteLine("[InternetShortcut]");
            writer.WriteLine("URL=file:///" + pathToExe);
            writer.WriteLine("IconIndex=0");
            string icon = pathToExe.Replace('\\', '/');
            writer.WriteLine("IconFile=" + icon);
        }
        

        当然,此代码未经测试,但在其他问题上已被接受,并且看起来正确。

        我在那个问题上看到了another answer,它列出了如何使用 Windows API 和一些 COM 互操作来完成它,但我很想回避这个问题,如果它有效,就使用上面的代码。这比任何事情都更符合个人喜好,我通常会为此使用预先建立的 API,但是当解决方案如此简单时,我只是不确定该选项到底有多值得。但是为了更好的衡量,我相信这段代码应该可以工作。当然,它完全未经测试。尤其是在这里你正在玩这样的东西,确保你在执行之前理解每一行。我不想看到你因为盲目跟踪我发布的代码而在系统上搞砸了。

        WshShell shell = new WshShell();
        IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(appStartMenuPath + ".lnk");
        shortcut.Description = "Shortcut for TestApp";
        shortcut.TargetPath = pathToExe;
        shortcut.Save();
        

        当然,您还需要对“Windows 脚本宿主对象模型”的引用,该引用可以在“添加引用”和“COM”下找到。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-02-23
          • 2010-10-27
          • 1970-01-01
          • 1970-01-01
          • 2018-09-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多