c#通過純程式碼建立案頭捷徑,程式功能表項目、將網頁添加到收藏夾的詳解

來源:互聯網
上載者:User
c#通過純程式碼建立案頭捷徑、建立程式功能表項目、將網頁添加到收藏夾


開始菜單》程式功能表項目:

添加到收藏夾:

相關函數代碼:

public const int SW_SHOWNORMAL = 1;        /// <summary>        /// 建立捷徑。        /// </summary>        /// <param name="shortcutPath">捷徑路徑。</param>        /// <param name="targetPath">目標路徑。</param>        /// <param name="workingDirectory">工作路徑。</param>        /// <param name="description">快速鍵描述。</param>        public static bool CreateShortcut(string shortcutPath, string targetPath, string workingDirectory, string description, string iconLocation = null)        {            try            {                CShellLink cShellLink = new CShellLink();                IShellLink iShellLink = (IShellLink)cShellLink;                iShellLink.SetDescription(description);                iShellLink.SetShowCmd(SW_SHOWNORMAL);                iShellLink.SetPath(targetPath);                iShellLink.SetWorkingDirectory(workingDirectory);                if (!string.IsNullOrEmpty(iconLocation))                {                    iShellLink.SetIconLocation(iconLocation, 0);                }                            IPersistFile iPersistFile = (IPersistFile)iShellLink;                iPersistFile.Save(shortcutPath, false);                Marshal.ReleaseComObject(iPersistFile);                iPersistFile = null;                Marshal.ReleaseComObject(iShellLink);                iShellLink = null;                Marshal.ReleaseComObject(cShellLink);                cShellLink = null;                return true;            }            catch //(System.Exception ex)            {                return false;            }        }


/// <summary>        /// 建立案頭捷徑        /// </summary>        /// <param name="targetPath">可執行檔路徑</param>        /// <param name="description">捷徑名稱</param>        /// <param name="iconLocation">捷徑表徵圖路徑</param>        /// <param name="workingDirectory">工作路徑</param>        /// <returns></returns>        public static bool CreateDesktopShortcut(string targetPath, string description, string iconLocation = null, string workingDirectory = null)        {            if (string.IsNullOrEmpty(workingDirectory))            {                workingDirectory = Shortcut.GetDeskDir();            }            return Shortcut.CreateShortcut(Shortcut.GetDeskDir() + "\\" + description + ".lnk", targetPath, workingDirectory, description, iconLocation);        }        /// <summary>        /// 建立程式菜單捷徑        /// </summary>        /// <param name="targetPath">可執行檔路徑</param>        /// <param name="description">捷徑名稱</param>        /// <param name="menuName">程式菜單中子功能表名稱,為空白則不建立子功能表</param>        /// <param name="iconLocation">捷徑表徵圖路徑</param>        /// <param name="workingDirectory">工作路徑</param>        /// <returns></returns>        public static bool CreateProgramsShortcut(string targetPath, string description, string menuName, string iconLocation = null, string workingDirectory = null)        {            if (string.IsNullOrEmpty(workingDirectory))            {                workingDirectory = Shortcut.GetProgramsDir();            }            string shortcutPath = Shortcut.GetProgramsDir();            if (!string.IsNullOrEmpty(menuName))            {                shortcutPath += "\\" + menuName;                if (!System.IO.Directory.Exists(shortcutPath))                {                    try                    {                        System.IO.Directory.CreateDirectory(shortcutPath);                    }                    catch //(System.Exception ex)                    {                        return false;                    }                }            }            shortcutPath += "\\" + description + ".lnk";            return Shortcut.CreateShortcut(shortcutPath, targetPath, workingDirectory, description, iconLocation);        }        /// <summary>        /// 將網頁添加到收藏夾        /// </summary>        /// <param name="url">要添加到收藏夾的網址</param>        /// <param name="description">標題</param>        /// <param name="folderName">收藏檔案夾名稱</param>        /// <param name="iconLocation">表徵圖檔案路徑</param>        /// <param name="workingDirectory">工作路徑</param>        /// <returns></returns>        public static bool AddFavorites(string url, string description, string folderName, string iconLocation = null, string workingDirectory = null)        {            if (string.IsNullOrEmpty(workingDirectory))            {                workingDirectory = Shortcut.GetProgramsDir();            }            string shortcutPath = Shortcut.GetFavoriteDir();            if (!string.IsNullOrEmpty(folderName))            {                shortcutPath += "\\" + folderName;                if (!System.IO.Directory.Exists(shortcutPath))                {                    try                    {                        System.IO.Directory.CreateDirectory(shortcutPath);                    }                    catch //(System.Exception ex)                    {                        return false;                    }                }            }            shortcutPath += "\\" + description + ".lnk";            return Shortcut.CreateShortcut(shortcutPath, url, workingDirectory, description, iconLocation);        }
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.