本文實例講述了C#實現在啟動目錄創建快捷方式的方法。分享給大家供大家參考。具體如下:
添加引用,選擇 COM 選項卡并選擇 Windows Script Host Object Model
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
/// <summary> /// 將文件放到啟動文件夾中開機啟動 /// </summary> /// <param name="setupPath">啟動程序</param> /// <param name="linkname">快捷方式名稱</param> /// <param name="description">描述</param> public void SetSetupWindowOpenRun( string setupPath, string linkname, string description) { string desktop = Environment.GetFolderPath(Environment.SpecialFolder.Startup) + "\\" + linkname + ".lnk" ; if (System.IO.File.Exists(desktop)) System.IO.File.Delete(desktop); IWshRuntimeLibrary.WshShell shell; IWshRuntimeLibrary.IWshShortcut shortcut; try { shell = new IWshRuntimeLibrary.WshShell(); shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(desktop); shortcut.TargetPath = setupPath; //程序路徑 shortcut.Arguments = "" ; //參數 shortcut.Description = description; //描述 shortcut.WorkingDirectory = System.IO.Path.GetDirectoryName(setupPath); //程序所在目錄 shortcut.IconLocation = setupPath; //圖標 shortcut.WindowStyle = 1; shortcut.Save(); } catch (Exception ex) { System.Windows.Forms.MessageBox.Show(ex.Message, "友情提示" ); } finally { shell = null ; shortcut = null ; } } |
希望本文所述對大家的C#程序設計有所幫助。