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\ Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice
HKEY_CURRENT_USER\ Software\Microsoft\Windows\Shell\Associations\UrlAssociations\https\UserChoice测试

Code:spa

class Program
{
    private const string HTTP_KEY = @"Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice";

    private const string HTTPS_KEY = @"Software\Microsoft\Windows\Shell\Associations\UrlAssociations\https\UserChoice";

    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会在右下角提示:code

通过调查分析,这是由于从Windows 10开始,修改ProgId的同时还须要修改Hash值,component

 

若是咱们经过手动的方式来修改Windows 10默认浏览器时会发现这个Hash值每次修改都会改变,并且不同。猜想这是由于微软不但愿有第三方程序来修改默认浏览器吧。经过注册表来修改默认浏览器的方式看来行不通了。blog

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

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

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

下面咱们就须要经过C#程序来承载这个测试脚本。要使脚本可以在客户机器上运行,咱们须要添加一些Coded UI的Assembly,string

1. 将下面DLL拷贝到 C:\Program Files (x86)\Common Files\Microsoft Shared\VSTT\14.0(32位地址:C:\Program Files\Common Files\Microsoft Shared\VSTT\14.0)

 

2. 注册C:\Program Files (x86)\Common Files\Microsoft Shared\VSTT\14.0\Microsoft.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设置为了默认浏览器。

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

相关文章
相关标签/搜索