1. 程式人生 > >C# 建立桌面快捷方式

C# 建立桌面快捷方式

/// <summary>
/// 建立桌面快捷方式
/// </summary>
/// <param name="deskTop">桌面的路徑</param>
/// <param name="FileName">檔案的名稱</param>
/// <param name="exePath">EXE的路徑</param>
/// <returns>成功或失敗</returns>
public bool CreateDesktopShortcut(string deskTop, string FileName, string exePath)
{
    try
    {
        string deskTop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\";
        if (System.IO.File.Exists(deskTop + FileName + ".lnk"))  //
        {
            System.IO.File.Delete(deskTop + FileName + ".lnk");//刪除原來的桌面快捷鍵方式
        }
        WshShell shell = new WshShell();

        //快捷鍵方式建立的位置、名稱
        IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(deskTop + FileName + ".lnk");
        shortcut.TargetPath = exePath; //目標檔案
        //該屬性指定應用程式的工作目錄,當用戶沒有指定一個具體的目錄時,快捷方式的目標應用程式將使用該屬性所指定的目錄來裝載或儲存檔案。
        shortcut.WorkingDirectory = System.Environment.CurrentDirectory;
        shortcut.WindowStyle = 1; //目標應用程式的視窗狀態分為普通、最大化、最小化【1,3,7】
        shortcut.Description = FileName; //描述
        shortcut.IconLocation = exePath + "\\logo.ico";  //快捷方式圖示
        shortcut.Arguments = "";
        //shortcut.Hotkey = "CTRL+ALT+F11"; // 快捷鍵
        shortcut.Save(); //必須呼叫儲存快捷才成建立成功
        return true;
    }
    catch (Exception)
    {
        return false;
    }
}