引用COM
Windows Script Host Object Model
然后把IWshRuntimeLibrary的引用-属性,嵌入互操作类型,改为false
参考代码如下
private static void CreateDesktopLnk() { string DesktopPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop);//得到桌面文件夹 IWshRuntimeLibrary.WshShell shell = new IWshRuntimeLibrary.WshShellClass(); IWshRuntimeLibrary.IWshShortcut shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(DesktopPath + "\\QQ软件.lnk"); shortcut.TargetPath = @"C:\Program Files\Tencent\QQ\Bin\QQ.exe"; shortcut.Arguments = "";// 参数 shortcut.Description = "自建的QQ快捷方式"; shortcut.WorkingDirectory = @"C:\Program Files\Tencent\QQ\Bin\";//程序所在文件夹,在快捷方式图标点击右键可以看到此属性 shortcut.IconLocation = @"C:\Program Files\Tencent\QQ\Bin\QQ.exe,0";//图标 //shortcut.Hotkey = "CTRL+SHIFT+X";//热键 shortcut.WindowStyle = 1; shortcut.Save(); /** 网站的快捷方式 */ IWshRuntimeLibrary.IWshShortcut shortcutWeb = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(DesktopPath + "\\www.lnk"); shortcutWeb.TargetPath = @"%HOMEDRIVE%/Program Files\Internet Explorer\IEXPLORE.EXE"; shortcutWeb.Arguments = "http://sosoft.cnblogs.com";// 参数 shortcutWeb.Description = "sosoft.cnblogs.com"; shortcutWeb.WorkingDirectory = Application.StartupPath;//程序所在文件夹,在快捷方式图标点击右键可以看到此属性 shortcutWeb.IconLocation = @"%HOMEDRIVE%/Program Files\Internet Explorer\IEXPLORE.EXE, 0";//图标 shortcutWeb.Hotkey = "CTRL+SHIFT+Z";//热键 shortcutWeb.WindowStyle = 1; shortcutWeb.Save(); }
当然我自己封装了一个类,方便调用
public static class MsiHelper
{
private static RegistryKey OpenRegistryPath(RegistryKey regKey, string regPath)
{
RegistryKey registryKey = regKey;
string text = regPath.Remove(0, 1) + "\\";
while (text.IndexOf("\\") != -1)
{
registryKey = registryKey.OpenSubKey(text.Substring(0, text.IndexOf("\\")));
text = text.Remove(0, text.IndexOf("\\") + 1);
}
return registryKey;
}
public static string GetCommonDesktopPath()
{
string regPath = "\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders";
object value = OpenRegistryPath(Registry.LocalMachine, regPath).GetValue("Common Desktop");
return (value != null) ? value.ToString() : string.Empty;
}
public static void CreateDesktopLink(string shortcutName, string targetPath, string workingDirectory, string arguments, string description)
{
try
{
string commonDesktopPath = GetCommonDesktopPath();
string text = string.Format("{0}\\{1}.lnk", commonDesktopPath, shortcutName);
if (!System.IO.File.Exists(text))
{
WshShell wshShell = new WshShellClass();
IWshShortcut wshShortcut = (IWshShortcut)wshShell.CreateShortcut(text);
wshShortcut.TargetPath = targetPath;
wshShortcut.Arguments = arguments;
wshShortcut.Description = description;
wshShortcut.WorkingDirectory = workingDirectory;
wshShortcut.IconLocation = string.Format("{0},0", targetPath);
wshShortcut.WindowStyle = 1;
wshShortcut.Save();
}
}
catch (Exception ex)
{
}
}
public static void CreateDesktopLink(string shortcutName, string targetPath, string workingDirectory, string arguments, string description,string IconLocation)
{
try
{
string commonDesktopPath = GetCommonDesktopPath();
string text = string.Format("{0}\\{1}.lnk", commonDesktopPath, shortcutName);
if (!System.IO.File.Exists(text))
{
WshShell wshShell = new WshShellClass();
IWshShortcut wshShortcut = (IWshShortcut)wshShell.CreateShortcut(text);
wshShortcut.TargetPath = targetPath;
wshShortcut.Arguments = arguments;
wshShortcut.Description = description;
wshShortcut.WorkingDirectory = workingDirectory;
wshShortcut.IconLocation = IconLocation;
wshShortcut.WindowStyle = 1;
wshShortcut.Save();
}
}
catch (Exception ex)
{
}
}
}
调用示例
MsiHelper.CreateDesktopLink(你的文本, string.Format("{0}{1}", AppDomain.CurrentDomain.BaseDirectory, AppDomain.CurrentDomain.FriendlyName), string.Format("{0}", AppDomain.CurrentDomain.BaseDirectory), 启动参数, desc);