条件:
COM下Windows Script Host Object Model,然后可以通过以下方法创建快捷方式。
方法:
bool CrtShortCut(string FilePath, string fileName)
{
//MessageBox.Show(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory).ToString());
//从COM中引用 Windows Script Host Object Model
//再using IWshRuntimeLibrary;
WshShell shell = new WshShell();
//创建桌面快捷方式
IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + "//" + fileName + ".lnk");
shortcut.TargetPath = FilePath;
shortcut.WorkingDirectory = Environment.CurrentDirectory;
shortcut.WindowStyle = 1;
shortcut.Description = fileName;
shortcut.Save();
//创建开始菜单快捷方式
IWshShortcut shortcut1 = (IWshShortcut)shell.CreateShortcut(Environment.GetFolderPath(Environment.SpecialFolder.StartMenu) + "//" + fileName + ".lnk");
shortcut1.TargetPath = FilePath;
shortcut1.WorkingDirectory = Environment.CurrentDirectory;
shortcut1.WindowStyle = 1;
shortcut1.Description = fileName;
shortcut1.Save();
return true;
}
调用:
CrtShortCut("c://a.exe", "a.exe");