如何用C#将exe文件的快捷方式生成到桌面上呢,请看下面的源码
首先需要引用window script host object model,
然后using IWshRuntimeLibrary;
using IWshRuntimeLibrary;
public bool CreateDesktopShortcut(string deskTop, string FileName, string exePath)
{
try
{
string deskTop1 = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\";
if (System.IO.File.Exists(deskTop + FileName + ".lnk")) //
{
System.IO.File.Delete(deskTop + FileName + ".lnk");//删除原来的桌面快捷键方式
}
WshShell shell = new WshShell();
//快捷键方式创建的位置、名称
IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(deskTop + FileName + ".lnk");
shortcut.TargetPath = exePath; //目标文件
//该属性指定应用程序的工作目录,当用户没有指定一个具体的目录时,快捷方式的目标应用程序将使用该属性所指定的目录来装载或保存文件。
shortcut.WorkingDirectory = System.Environment.CurrentDirectory;
shortcut.WindowStyle = 1; //目标应用程序的窗口状态分为普通、最大化、最小化【1,3,7】
shortcut.Description = FileName; //描述
shortcut.IconLocation = exePath + "\\logo.ico"; //快捷方式图标
shortcut.Arguments = "";
//shortcut.Hotkey = "CTRL+ALT+F11"; // 快捷键
shortcut.Save(); //必须调用保存快捷才成创建成功
return true;
}
catch (Exception)
{
return false;
}
}
如何调用该方法呢:可以写一个button click事件
private void button3_Click(object sender, EventArgs e)
{
string deskTop = @"C:\Users\abc\Desktop\";
string FileName = "a.lnk";
string exePath = @"C:\abc.exe";
bool res=CreateDesktopShortcut(deskTop, FileName, exePath);
Console.WriteLine(res.ToString());
}
调用该方法传参说明:
deskTop:桌面的路径
FileName: 文件名称
exePath: exe文件的路径