• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

c#通过纯代码创建桌面快捷方式,程序菜单项、将网页添加到收藏夹的详解(图)

c# 搞代码 4年前 (2022-01-09) 23次浏览 已收录 0个评论

c#通过纯代码创建桌面快捷方式、创建程序菜单项、将网页添加到收藏夹

开始菜单》程序菜单项:

添加到收藏夹:

相关函数代码:

public const int SW_SHOWNORMAL = 1;        /// <summary>        /// 创建快捷方式。        /// </<i style="color:transparent">本文来源gaodai$ma#com搞$$代**码)网8</i>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);        }

以上就是c#通过纯代码创建桌面快捷方式,程序菜单项、将网页添加到收藏夹的详解(图)的详细内容,更多请关注搞代码gaodaima其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:c#通过纯代码创建桌面快捷方式,程序菜单项、将网页添加到收藏夹的详解(图)
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址