C# WPF 创建桌面快捷方式

目前搜索到的文章里面就直接一行 WshShell shell = new WshShell(); 就结束了,没有说明如何引用COM组件 添加COM: 在项目的依赖项中右键 搜索:Windows Script Host Object Model 搜索对应COM组件</

目前搜索到的文章里面就直接一行

WshShell shell = new WshShell();

就结束了,没有说明如何引用COM组件

添加COM:

在项目的依赖项中右键

搜索:Windows Script Host Object Model

搜索对应COM组件

确定即可

// 获取当前可执行文件的路径
string executablePath = Assembly.GetExecutingAssembly().Location;

// 获取桌面路径
string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);

// 创建快捷方式文件的路径
string shortcutPath = Path.Combine(desktopPath, "MyAppShortcut.lnk");

// 创建 WshShell 对象
WshShell shell = new WshShell();

// 创建快捷方式
IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutPath);

// 设置快捷方式属性
shortcut.TargetPath = executablePath;
shortcut.WorkingDirectory = Path.GetDirectoryName(executablePath);
shortcut.Description = "My WPF App Shortcut";
shortcut.IconLocation = executablePath + ",0"; // 使用应用程序图标

// 保存快捷方式
shortcut.Save();

注意:

添加引用后,可能会导致File的引用不明确,建议添加using避免冲突:

using File = System.IO.File;

LICENSED UNDER CC BY-NC-SA 4.0
Comment