`
testcs_dn
  • 浏览: 104708 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

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

 
阅读更多

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

分享到:
评论

相关推荐

    通过纯代码创建快捷方式.zip

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

    C#编程经验技巧宝典

    2 &lt;br&gt;0003 设置程序代码行序号 3 &lt;br&gt;0004 开发环境全屏显示 3 &lt;br&gt;0005 设置窗口的自动隐藏功能 3 &lt;br&gt;0006 根据需要创建所需解决方案 4 &lt;br&gt;0007 如何使用“验证的目标架构”功能 4 ...

    C#浏览器编程,学习使用

    //桌面快捷方式ID public int ID_IE_FILE_NEWMAIL = 279; //新建邮件ID public uint WM_COMMAND = 0x0111; //***********************************************// // // // 常用变量声明 // // // //****...

    vc++ 应用源码包_1

    VC++建立桌面或开始菜单快捷方式 VC++界面库编程 SkinMagic 2.21 动态库版本的使用和 Skin++动态库及静态库版本的使用。 VC++精仿QQ2008窗体及分类菜单 VC++卡通风格气泡提示源码 VC++实现任务管理器源码 任务...

    vc++ 应用源码包_6

    VC++建立桌面或开始菜单快捷方式 VC++界面库编程 SkinMagic 2.21 动态库版本的使用和 Skin++动态库及静态库版本的使用。 VC++精仿QQ2008窗体及分类菜单 VC++卡通风格气泡提示源码 VC++实现任务管理器源码 任务...

    vc++ 应用源码包_5

    VC++建立桌面或开始菜单快捷方式 VC++界面库编程 SkinMagic 2.21 动态库版本的使用和 Skin++动态库及静态库版本的使用。 VC++精仿QQ2008窗体及分类菜单 VC++卡通风格气泡提示源码 VC++实现任务管理器源码 任务...

    vc++ 应用源码包_2

    VC++建立桌面或开始菜单快捷方式 VC++界面库编程 SkinMagic 2.21 动态库版本的使用和 Skin++动态库及静态库版本的使用。 VC++精仿QQ2008窗体及分类菜单 VC++卡通风格气泡提示源码 VC++实现任务管理器源码 任务...

Global site tag (gtag.js) - Google Analytics