核心代码
using System;
using System.IO;
using System.Runtime.InteropServices;
using IWshRuntimeLibrary; // 需要添加对IWshRuntimeLibrary的引用 在nuget里面安装Interop.IWshRuntimeLibrary
namespace DesktopShortcutCreator
{
class Program
{
static void Main(string[] args)
{
CreateShortcut()
Console.ReadLine();
}
static void CreateShortcut()
{
try
{
// 检查桌面上是否已存在快捷方式
string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
string shortcutPath = Path.Combine(desktopPath, "应用程序名称.lnk");
if (!System.IO.File.Exists(shortcutPath))//如果桌面没有快捷方式,则创建
{
// 获取当前应用程序的可执行文件路径
string targetPath = System.Reflection.Assembly.GetExecutingAssembly().Location;
// 创建WshShell对象
WshShell shell = new WshShell();
IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutPath);
// 设置快捷方式属性
shortcut.TargetPath = targetPath;
shortcut.WorkingDirectory = Path.GetDirectoryName(targetPath);
shortcut.Description = "应用程序描述";
shortcut.IconLocation = targetPath + ",0"; // 使用应用程序自身的图标
// 保存快捷方式
shortcut.Save();
}
}
catch (Exception ex)
{
throw ex;
}
}
}
}视频教程,进入B站在可以看高清视频
本文来自 www.luofenming.com