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設置爲了默認瀏覽器。

感謝您的閱讀,代碼點擊這裏下載。若是您有其餘方法,歡迎與我分享。

相關文章
相關標籤/搜索