You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在.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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 08:48:10