C#创建桌面快捷方式
时间: 2024-01-06 19:24:21 浏览: 362
以下是使用C#创建桌面快捷方式的示例代码:
```csharp
using IWshRuntimeLibrary;
public bool CreateDesktopShortcut(string desktopPath, string shortcutName, string targetPath)
{
try
{
WshShell shell = new WshShell();
IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(desktopPath + "\\" + shortcutName + ".lnk");
shortcut.TargetPath = targetPath;
shortcut.Save();
return true;
}
catch (Exception ex)
{
Console.WriteLine("创建快捷方式失败:" + ex.Message);
return false;
}
}
```
你可以调用上述方法来创建桌面快捷方式。方法的参数包括桌面路径、快捷方式的备注信息和需创建快捷方式程序的路径。如果创建成功,方法将返回true;如果创建失败,方法将返回false。
相关问题
c#创建桌面快捷方式
在C#中创建桌面快捷方式,可以使用WshShellClass类和IWshShortcut接口。以下是创建桌面快捷方式的步骤:
1. 引用Windows Script Host Object Model,添加COM引用WScript.Shell。
2. 创建WshShellClass对象。
3. 使用CreateShortcut方法创建快捷方式对象。
4. 设置快捷方式的属性,例如TargetPath、WorkingDirectory、IconLocation等。
5. 使用Save方法保存快捷方式。
需要注意的是,如果要使用System.IO程序集中的File类,需要以全名引用之。同时,不要直接引用Interop.IWshRuntimeLibrary.dll程序集,因为它可能是x86或x64的,如果程序以Any CPU方式编译,则它不再有适配性。
以下是一个示例代码:
```
using IWshRuntimeLibrary;
// 创建桌面快捷方式
public static void CreateShortcut(string shortcutName, string targetPath, string workingDirectory, string iconPath)
{
// 创建WshShellClass对象
WshShellClass wshShell = new WshShellClass();
// 使用CreateShortcut方法创建快捷方式对象
IWshShortcut shortcut = (IWshShortcut)wshShell.CreateShortcut(
Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + "\\" + shortcutName + ".lnk");
// 设置快捷方式的属性
shortcut.TargetPath = targetPath;
shortcut.WorkingDirectory = workingDirectory;
shortcut.IconLocation = iconPath;
// 保存快捷方式
shortcut.Save();
}
```
winform c#通过桌面快捷方式打开exe外部程序
你可以使用System.Diagnostics.Process类来启动外部程序。以下是一个示例代码:
```csharp
Process.Start("your_program.exe");
```
如果你想要通过桌面快捷方式打开程序,你可以在桌面创建一个快捷方式,然后在代码中指定快捷方式的路径:
```csharp
Process.Start(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\your_shortcut.lnk");
```
请注意,这需要快捷方式的实际路径和名称,而不是快捷方式指向的文件的路径和名称。
阅读全文
相关推荐














