C#如何利用 Windows Script Host Object Mode COM组件来实现快捷方式的创建生成
不多说废话 开整
1.首先引用COM组件 “Windows Script Host Object Model”
2.再引用命名空间 IWshRuntimeLibrary
3.将这个下面这个类拷贝进项目(已做好封装 相应调用方法即可)
public class ShortcutManager { WshShell shell = new WshShell(); string ShortFileName; public ShortcutManager(string ShortFileName) { this.ShortFileName = ShortFileName; } ////// 创建快捷方式 /// /// 快捷方式文件名称 无需后缀名 /// 快捷方式文件存储位置 public void Create(string ShortName, string ShortFileDirectory) { if (!System.IO.Directory.Exists(ShortFileDirectory)) { System.IO.Directory.CreateDirectory(ShortFileDirectory); } string shortcutPath = Path.Combine(ShortFileDirectory, string.Format("{0}.lnk", ShortName)); if (System.IO.File.Exists(shortcutPath)) { return; } IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutPath);//创建快捷方式对象 shortcut.TargetPath = ShortFileName;//指定目标路径 shortcut.WorkingDirectory = Path.GetDirectoryName(ShortFileName);//设置起始位置 shortcut.WindowStyle = 1;//设置运行方式,默认为常规窗口 shortcut.IconLocation = ShortFileName; shortcut.Save();//保存快捷方式 }
调用例子 存储到桌面的快捷方式
string exe = this.GetType().Assembly.Location;//当前程序的完整路径 ShortcutManager manager = new ShortcutManager(exe); string desktop=System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop); manager.Create("我的快捷方式", desktop);