如何使用C#操作快捷方式(獲取快捷方式屬性、創建快捷方式)


近來項目中有需要用到一個技術:使用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 謝謝。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM