如何创建开始菜单快捷方式

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

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。

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

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

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

注意:要使用Windows脚本宿主库 ,需要在“ 参考”>“COM”选项卡>“Windows脚本宿主对象模型”下添加引用。

有关详细信息,请参阅此文章: 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(); } 

这与此问题几乎相同: 在桌面C#上创建快捷方式 。

要从该答案中复制,您需要自己创建快捷方式文件。

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

当然,这段代码是未经测试的,但是在其他问题上它被接受并且它看起来是正确的。

我在这个问题上看到了另一个答案 ,其中列出了如何使用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”。