首先,导入COM组件Windows Script Host Object Model
其次,引用命名空间using IWshRuntimeLibrary;
//判断创建快捷键方式桌面是否存在
string deskTop = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop);
if(System.IO.File.Exists(deskTop + "\\上传更新程序.lnk")) //
{
// System.IO.File.Delete(deskTop + "\\上传更新程序.lnk");//删除原来的桌面快捷键方式
return;
}
//注:如果桌面有现准备创建的快捷键方式,当程序执行创建语句时会修改桌面已有快捷键方式,程序不会出现异常
WshShell shell = new WshShell();
//快捷键方式创建的位置、名称
IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut( Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + "\\" + "上传更新程序.lnk");
shortcut.TargetPath = @Application.StartupPath+"\\auto.AutoTranUpdate.exe"; //目标文件
shortcut.WorkingDirectory = System.Environment.CurrentDirectory;//该属性指定应用程序的工作目录,当用户没有指定一个具体的目录时,快捷方式的目标应用程序将使用该属性所指定的目录来装载或保存文件。
shortcut.WindowStyle = 1; //目标应用程序的窗口状态分为普通、最大化、最小化【1,3,7】
shortcut.Description = "自动更新程序"; //描述
shortcut.IconLocation = Application.StartupPath + "\\App.ico"; //快捷方式图标
shortcut.Arguments = "";
shortcut.Hotkey = "CTRL+ALT+F11"; // 快捷键
shortcut.Save(); //必须调用保存快捷才成创建成功