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;
    }
}


免責聲明!

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



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