(轉)Winform 創建桌面快捷方式並開機啓動

快捷方式實質上是一個擴展名爲 .LNK 的文件

方法如下:

首先要添加引用 (如圖)

 

 

就是那個Windows Script Host Object Model的類庫....

 

然後在程序中引入命名空間

using IWshRuntimeLibrary;

 有一些文件操作,所有要引入

using System.IO;

關鍵方法如下:

/// <summary>
/// 創建桌面快捷方式並開機啓動的方法
/// </summary>
private void ShortcutAndStartup()
{
     
//獲取當前系統用戶啓動目錄
     string startupPath = Environment.GetFolderPath(Environment.SpecialFolder.Startup);
     
//獲取當前系統用戶桌面目錄
     string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);

     FileInfo fileStartup 
= new FileInfo(startupPath + "//億掌通.lnk");
     FileInfo fileDesktop 
= new FileInfo(desktopPath + "//億掌通.lnk");

     
if (!fileDesktop.Exists)
     {
           WshShell shell 
= new WshShell();
           IWshShortcut shortcut 
= (IWshShortcut)shell.CreateShortcut(
                 Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) 
+
                  
"//" + "億掌通.lnk"
                  );
           shortcut.TargetPath 
= Application.StartupPath + "//" + "Upgrade.exe";//啓動更新程序★
           shortcut.WorkingDirectory = System.Environment.CurrentDirectory;
           shortcut.WindowStyle 
= 1;
           shortcut.Description 
= "億掌通";
           shortcut.IconLocation 
= Application.ExecutablePath;
           shortcut.Save();
      }

      
if (!fileStartup.Exists)
      {
            
//獲取可執行文件快捷方式的全部路徑
            string exeDir = desktopPath + "//億掌通.lnk";
            
//把程序快捷方式複製到啓動目錄
            System.IO.File.Copy(exeDir, startupPath + "//億掌通.lnk"true);
      }
}
發佈了160 篇原創文章 · 獲贊 7 · 訪問量 66萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章