コード例 #1
0
        /// <summary>Get the default browser by protocol (eg. HTTP, HTTPS, FTP, SMS, MailTo, ...)</summary>
        /// <param name="browsers">When you already called GetInstalledBrowsers(), pass it here.</param>
        /// <param name="protocolType">Protocol type</param>
        /// <returns></returns>
        public static Browser GetDefaultBrowser(IEnumerable <Browser> browsers, Enums.eProtocolType protocolType)
        {
            var urlAssociationsKey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\Shell\Associations\URLAssociations");
            var protocolName       = Enum.GetName(typeof(Enums.eProtocolType), protocolType)?.ToLower(CultureInfo.InvariantCulture);

            if (urlAssociationsKey != null && !urlAssociationsKey.GetSubKeyNames().Contains(protocolName))
            {
                throw new BrowserException($"No url association for {protocolName}");
            }

            var userChoiceKey        = urlAssociationsKey?.OpenSubKey($@"{protocolName}\UserChoice");
            var defaultBrowserProgId = userChoiceKey?.GetValue("ProgId");

            var foundNormalBrowser = browsers.FirstOrDefault(
                b => b.UrlAssociations.Any(
                    a => (a.Key == protocolName) && a.Value.Equals(defaultBrowserProgId)
                    )
                );

            if (foundNormalBrowser != null)
            {
                return(foundNormalBrowser);
            }
            else
            {
                switch (defaultBrowserProgId)
                {
                case "AppX4hxtad77fbk3jkkeerkrm0ze94wjf3s9":     // htm, html
                case "AppXd4nrz8ff68srnhf9t5a8sbjyar1cr723":     // pdf
                case "AppXde74bfzw9j31bzhcvsrxsyjnhhbq66cs":     // svg
                case "AppXcc58vyzkbjbs4ky0mxrmxf8278rk9b3t":     // xml
                case "AppXq0fevzme2pys62n3e0fbqa7peapykr8v":
                    // Old Edge
                    return(browsers.FirstOrDefault(
                               b => ((b.KeyName == "Microsoft Edge") && b.IsApplicationGenerated)
                               ));

                case "IE.HTTP":
                    // Internet Explorer
                    return(browsers.FirstOrDefault(
                               b => b.KeyName == "IEXPLORE.EXE"
                               ));

                default:
                    return(null);
                }
            }
        }
コード例 #2
0
        /// <summary>Get the default browser by protocol (eg. HTTP, HTTPS, FTP, SMS, MailTo, ...).</summary>
        /// <param name="protocolType">Protocol type</param>
        /// <returns></returns>
        public static Browser GetDefaultBrowser(Enums.eProtocolType protocolType)
        {
            var browsers = GetInstalledBrowsers();

            return(GetDefaultBrowser(browsers, protocolType));
        }