c#创建指定exe的快捷方式到指定路径下 给出最优的方式 并给出完整的代码
时间: 2024-12-15 12:31:11 浏览: 22
在C#中,创建一个可执行文件(.exe)的快捷方式并将其放置在特定路径下,可以使用`System.IO`和`System.Windows.Forms`命名空间下的类。这里是一个简单的方法,使用`Process.Start`和`RegistryKey`来创建快捷方式:
```csharp
using System;
using System.IO;
using Microsoft.Win32;
public static void CreateShortcut(string targetPath, string shortcutName, string destinationPath)
{
// 创建新快捷方式的完整路径
string shortcutFilePath = Path.Combine(destinationPath, shortcutName + ".lnk");
// 使用注册表键来创建快捷方式
RegistryKey shortcutKey = Registry.CurrentUser.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Uninstall\" + Path.GetFileNameWithoutExtension(targetPath));
// 设置快捷方式属性
shortcutKey.SetValue("DisplayName", Path.GetFileName(targetPath));
shortcutKey.SetValue("FriendlyName", shortcutName);
shortcutKey.SetValue("URLInfoAbout", targetPath);
// 创建WPF快捷方式对象
var shortcut = new WshShell();
IWshRuntimeLibrary.IWshShortcut shortcutObject = shortcut.CreateShortcut(shortcutFilePath);
// 设置快捷方式的目标、工作目录和图标
shortcutObject.TargetPath = targetPath;
shortcutObject.WorkingDirectory = Environment.CurrentDirectory;
shortcutObject.IconLocation = Path.Combine(Environment.CurrentDirectory, "icon.ico"); // 如果有自定义图标,替换为实际路径
// 保存并关闭快捷方式
shortcutObject.Save();
Console.WriteLine($"创建快捷方式成功,位于: {shortcutFilePath}");
}
// 调用函数
CreateShortcut("path_to_your_exe.exe", "YourShortcutName", @"C:\DesiredDestinationFolder");
```
在这个例子中,你需要替换`targetPath`为你想要创建快捷方式的实际.exe文件路径,`shortcutName`是你想给快捷方式起的名字,以及`destinationPath`是你要将快捷方式放的目录。
阅读全文
相关推荐


















