如何在.NET 6 WinForms应用ClickOnce安装后生成桌面快捷方式?
解决ClickOnce发布后自动创建桌面快捷方式的问题
针对.NET 6 Windows Forms应用通过ClickOnce发布后自动生成桌面快捷方式,有两种可靠实现方案:
方式一:通过Visual Studio发布配置直接设置
- 右键项目,打开发布界面
- 点击右上角的选项按钮
- 在弹出窗口选择部署标签页
- 勾选创建桌面快捷方式选项
- 保存配置后重新发布,生成的setup.exe完成安装后会自动在桌面创建应用快捷方式
方式二:代码手动创建(适合自定义场景)
如果上述配置不生效,可在应用首次启动时添加代码生成快捷方式:
using IWshRuntimeLibrary; // 需添加COM引用:Windows Script Host Object Model using System.IO; using System.Reflection; private void CreateDesktopShortcut() { string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory); string shortcutPath = Path.Combine(desktopPath, "你的应用名称.lnk"); // 避免重复创建 if (File.Exists(shortcutPath)) return; WshShell shell = new WshShell(); IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutPath); shortcut.TargetPath = Assembly.GetExecutingAssembly().Location; shortcut.IconLocation = Assembly.GetExecutingAssembly().Location; // 使用应用自身图标 shortcut.Description = "你的应用描述"; shortcut.Save(); }
在主窗体Load事件或Program.cs中调用,同时标记首次启动状态避免重复执行:
if (!Properties.Settings.Default.IsFirstLaunch) { CreateDesktopShortcut(); Properties.Settings.Default.IsFirstLaunch = true; Properties.Settings.Default.Save(); }
注:使用代码方式需在项目中添加Windows Script Host Object Model的COM引用(右键项目→添加→引用→COM→勾选对应项)。
内容的提问来源于stack exchange,提问作者matsrom
相关产品推荐
相关产品推荐

