1. 程式人生 > >c 通過純程式碼建立桌面快捷方式 建立程式選單項 將網頁新增到收藏夾

c 通過純程式碼建立桌面快捷方式 建立程式選單項 將網頁新增到收藏夾

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow

也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!

               

c#通過純程式碼建立桌面快捷方式、建立程式選單項、將網頁新增到收藏夾

本文章原始碼Src下載地址:http://download.csdn.net/detail/testcs_dn/5141580

開始選單》程式選單項:

新增到收藏夾:

 

相關函式程式碼:

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


 

本文章原始碼Src下載地址:http://download.csdn.net/detail/testcs_dn/5141580

 

           

給我老師的人工智慧教程打call!http://blog.csdn.net/jiangjunshow

這裡寫圖片描述