zoukankan      html  css  js  c++  java
  • Windows 10 使用C#如何将IE设置为默认浏览器

    在WPF XBAP项目中遇到这样一个问题,程序在Windows 10上面无法运行。原因是因为Windows 10默认浏览器是Edge,而XBAP程序是需要在IE上面运行的。于是开始想办法修改Windows 10的默认浏览器。在Windows 10之前,只需要修改注册表的就可以了。将下面注册表的ProgId设置为IE.HTTP/IE.HTTPS即可。

    HKEY_CURRENT_USER SoftwareMicrosoftWindowsShellAssociationsUrlAssociationshttpUserChoice
    HKEY_CURRENT_USER SoftwareMicrosoftWindowsShellAssociationsUrlAssociationshttpsUserChoice

    Code:

    class Program
    {
        private const string HTTP_KEY = @"SoftwareMicrosoftWindowsShellAssociationsUrlAssociationshttpUserChoice";
    
        private const string HTTPS_KEY = @"SoftwareMicrosoftWindowsShellAssociationsUrlAssociationshttpsUserChoice";
    
        static void Main(string[] args)
        {
            Console.WriteLine("Change default browser to IE");
    
            KeyChange(HTTP_KEY, false);
    
            KeyChange(HTTPS_KEY, true);
    
            Console.WriteLine("Changed successfully.");
    
            Console.ReadKey();
        }
    
        private static void KeyChange(string key, bool https = false)
        {
            using (RegistryKey subKey = Registry.CurrentUser.OpenSubKey(key,true))
            {
                if (subKey != null &&
                    subKey.GetValue("ProgId") != null)
                {
                    if (https)
                    {
                        if (subKey.GetValue("ProgId").ToString().ToUpper() != "IE.HTTPS")
                        {
                            subKey.SetValue("ProgId", "IE.HTTPS");
                        }
                    }
                    else
                    {
                        if (subKey.GetValue("ProgId").ToString().ToUpper() != "IE.HTTP")
                        {
                            subKey.SetValue("ProgId", "IE.HTTP");
                        }
                    }
                }
            }
        }
    }

    执行完成后,Windows 10会在右下角提示:

    经过调查分析,这是因为从Windows 10开始,修改ProgId的同时还需要修改Hash值,

     

    如果我们通过手动的方式来修改Windows 10默认浏览器时会发现这个Hash值每次修改都会改变,而且不一样。猜测这是因为微软不希望有第三方程序来修改默认浏览器吧。通过注册表来修改默认浏览器的方式看来行不通了。

    因为我们可以手动通过 控制面板 --> 默认程序 --> 选择IE浏览器 -->设置IE为默认浏览器来修改。这就提供了另外一个解决方案,通过录制一些脚本来执行。对Visual Studio Coded UI有一丁点儿的了解,于是我先使用Coded UI录制了修改默认浏览器的脚本。关于Coded UI的更多内容,请参考MSDN官网,

    https://msdn.microsoft.com/en-us/library/dd286726.aspx#VerifyingCodeUsingCUITCreate

    脚本点击这里下载,需要注意的是,需要使用Visual Studio 2015 Enterprise版本才能打开/运行Coded UI脚本。

    下面我们就需要通过C#程序来承载这个测试脚本。要使脚本能够在客户机器上运行,我们需要添加一些Coded UI的Assembly,

    1. 将下面DLL拷贝到 C:Program Files (x86)Common FilesMicrosoft SharedVSTT14.0(32位地址:C:Program FilesCommon FilesMicrosoft SharedVSTT14.0)

    2. 注册C:Program Files (x86)Common FilesMicrosoft SharedVSTT14.0Microsoft.VisualStudio.TestTools.UITest.Playback.Engine.dll

    private static void RegisterDll(string path)
    {
        try
        {
            //'/s' : Specifies regsvr32 to run silently and to not display any message boxes.
    
            string args = "/s" + " " + """ + path + """;
    
            Process process = new Process();
    
            //This file registers .dll files as command components in the registry.
            process.StartInfo.FileName = "regsvr32.exe";
    
            process.StartInfo.Arguments = args;
    
            process.StartInfo.UseShellExecute = false;
    
            process.StartInfo.CreateNoWindow = true;
    
            process.StartInfo.RedirectStandardOutput = true;
    
            process.Start();
    
            process.WaitForExit();
    
            process.Close();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.StackTrace);
        }
    }

    3. C#调用Coded UI脚本,

        Playback.Initialize();
    
        SetBrowserCodedUITest coded = new SetBrowserCodedUITest();
    
        coded.SetBrowserMethod();
    
        Playback.Cleanup();

    运行结果如下:

    通过测试,我们成功的将IE设置为了默认浏览器。

    感谢您的阅读,代码点击这里下载。如果您有其他方法,欢迎与我分享。

  • 相关阅读:
    Spring REST
    Spring整合CXF,发布RSETful 风格WebService
    ZT:阿里合伙人发文:十年磨一剑,自研数据库终拿世界第一
    转载:OutOfMemoryError系列(2): GC overhead limit exceeded
    SpringBoot/SpringMVC 下载本地文件
    Eclipse中查找接口实现类快捷键
    [java]察看两个日期间差多少秒/小时/天
    MongoDB(mongodb-win32-x86_64-enterprise-windows-64-4.2.1-signed.msi)下载,启动和插入数据,查询
    简繁瘦金体下载
    方正宋刻本秀楷字体下载
  • 原文地址:https://www.cnblogs.com/yang-fei/p/6044128.html
Copyright © 2011-2022 走看看