判断用户使用的默认浏览器

作者:追风剑情 发布于:2019-3-24 23:25 分类:C#

示例

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApp2
{
    class Program
    {
        public enum BrowserApplication
        {
            Unknown,
            InternetExplorer,
            Firefox,
            Chrome,
            Opera,
            Safari,
            Edge,
        }

        static void Main(string[] args)
        {
            BrowserApplication currentBrowser = GetDefaultBrowser();

            Console.WriteLine("当前使用的默认浏览器为: {0}", currentBrowser);

            Console.Read();
        }

        public static BrowserApplication GetDefaultBrowser()
        {
            //通过检查注册表(regedit)项来判断用户使用的默认浏览器
            //HKEY_CURRENT_USER\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice\ProgId
            const string userChoice = @"Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice";
            using (Microsoft.Win32.RegistryKey userChoiceKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(userChoice))
            {
                if (userChoiceKey == null)
                {
                    return BrowserApplication.Unknown;
                }

                object progIdValue = userChoiceKey.GetValue("Progid");
                if (progIdValue == null)
                {
                    return BrowserApplication.Unknown;
                }

                string browserId = progIdValue.ToString().ToLower();

                if (browserId.Contains("ie.http"))
                    return BrowserApplication.InternetExplorer;
                else if (browserId.Contains("firefox"))
                    return BrowserApplication.Firefox;
                else if (browserId.Contains("chrome"))
                    return BrowserApplication.Chrome;
                else if (browserId.Contains("opera"))
                    return BrowserApplication.Opera;
                else if (browserId.Contains("safari"))
                    return BrowserApplication.Safari;
                else if (browserId.Contains("appcq0fevzme2pys62n3e0fbqa7peapykr8v")) //AppXq0fevzme2pys62n3e0fbqa7peapykr8v
                    return BrowserApplication.Edge;
                else
                    return BrowserApplication.Unknown;
            }
        }
    }
}

运行测试

1111.png

2222.png

标签: C#

Powered by emlog  蜀ICP备18021003号   sitemap

川公网安备 51019002001593号