>  기사  >  백엔드 개발  >  C# 순수코드를 통한 바탕화면 바로가기 생성, 프로그램 메뉴 항목, 즐겨찾기에 웹페이지 추가에 대한 자세한 설명(그림)

C# 순수코드를 통한 바탕화면 바로가기 생성, 프로그램 메뉴 항목, 즐겨찾기에 웹페이지 추가에 대한 자세한 설명(그림)

黄舟
黄舟원래의
2017-03-15 10:12:193543검색

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


  

위 내용은 C# 순수코드를 통한 바탕화면 바로가기 생성, 프로그램 메뉴 항목, 즐겨찾기에 웹페이지 추가에 대한 자세한 설명(그림)의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.