c#通过纯代码创建桌面快捷方式、创建程序菜单项、将网页添加到收藏夹
开始菜单》程序菜单项:
添加到收藏夹:
相关函数代码:
public const int SW_SHOWNORMAL = 1; ////// 创建快捷方式。 /// /// 快捷方式路径。 /// 目标路径。 /// 工作路径。 /// 快捷键描述。 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; } }
登录后复制
////// 创建桌面快捷方式 /// /// 可执行文件路径 /// 快捷方式名称 /// 快捷方式图标路径 /// 工作路径 /// 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); } ////// 创建程序菜单快捷方式 /// /// 可执行文件路径 /// 快捷方式名称 /// 程序菜单中子菜单名称,为空则不创建子菜单 /// 快捷方式图标路径 /// 工作路径 /// 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); } ////// 将网页添加到收藏夹 /// /// 要添加到收藏夹的网址 /// 标题 /// 收藏文件夹名称 /// 图标文件路径 /// 工作路径 /// 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#通过纯代码创建桌面快捷方式,程序菜单项、将网页添加到收藏夹的详解(图)的详细内容,更多请关注【创想鸟】其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。
发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2549444.html