近来项目中有需要用到一个技术:使用C#操控快捷方式,包含创建和读取等。现整理一下实现方式,分享给大家。

第一步  创建一个项目

无需废话,跳过。

第二步  引用COM组件

右键“引用”,“添加引用”,选择“COM组件”,找到“Windows Script Host Object Model”,然后确定。

第三步  编写创建快捷方式的代码

创建快捷方式
 1 // 声明操作对象
 2 IWshRuntimeLibrary.WshShell shell = new IWshRuntimeLibrary.WshShellClass();
 3 // 创建一个快捷方式
 4 IWshRuntimeLibrary.IWshShortcut shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut("c:\\yeaicc.lnk");
 5 // 关联的程序
 6 shortcut.TargetPath = "notepad.exe";
 7 // 参数
 8 shortcut.Arguments = "c:\\yeaicc.txt";
 9 // 快捷方式描述,鼠标放到快捷方式上会显示出来哦
10 shortcut.Description = "我的快捷方式--yeaicc";
11 // 全局热键
12 shortcut.Hotkey = "CTRL+SHIFT+N";
13 // 设置快捷方式的图标,这里是取程序图标,如果希望指定一个ico文件,那么请写路径。
14 shortcut.IconLocation = "notepad.exe, 0";
15 // 保存,创建就成功了。
16 shortcut.Save();

第四步  读取快捷方式属性

获取快捷方式属性
1 IWshRuntimeLibrary.WshShell shell = new IWshRuntimeLibrary.WshShellClass();
2 IWshRuntimeLibrary.IWshShortcut shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut("c:\\yeaicc.lnk");
3 // 亲,根据刚刚创建时的代码,你想获取什么属性?
4 MessageBox.Show(ws.Description);

第五步  注意

转载请注明:本文来自博客园--yeaicc 谢谢。