簡體   English   中英

如何使用C#在Windows 7上設置默認瀏覽器?

[英]How to set the default browser on Windows 7 using c#?

我正在編寫一個方法,該方法將接收瀏覽器名稱並將系統默認值更改為主要瀏覽器之一:

public static void SetSystemDefaultBrowser(string aBrowserName)
{
    if (aBrowserName.ToLower() == GetSystemDefaultBrowser().ToLower())
        return;

    switch (aBrowserName.ToLower())
    {
        case "firefox":
            Registry.ClassesRoot.OpenSubKey(@".htm", true).SetValue("", "FirefoxHTML");
            Registry.ClassesRoot.OpenSubKey(@".html", true).SetValue("", "FirefoxHTML");
            Registry.ClassesRoot.OpenSubKey(@".shtml", true).SetValue("", "FirefoxHTML");
            Registry.ClassesRoot.OpenSubKey(@".xht", true).SetValue("", "FirefoxHTML");
            Registry.ClassesRoot.OpenSubKey(@".xhtml", true).SetValue("", "FirefoxHTML");
            Registry.ClassesRoot.OpenSubKey(@"http\shell\open\command", true).SetValue("", "firefox.exe");
            Registry.ClassesRoot.OpenSubKey(@"https\shell\open\command", true).SetValue("", "firefox.exe");
            Registry.CurrentUser.OpenSubKey(@"Software\Classes\http\shell\open\command", true).SetValue("", "firefox.exe");
            Registry.CurrentUser.OpenSubKey(@"Software\Classes\https\shell\open\command", true).SetValue("", "firefox.exe");
            Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice", true).SetValue("progId", "FirefoxURL");
            Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\Shell\Associations\UrlAssociations\https\UserChoice", true).SetValue("progId", "FirefoxURL");
            Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\Shell\Associations\UrlAssociations\ftp\UserChoice", true).SetValue("progId", "FirefoxURL");    
            break;
        case "chrome":
            Process.Start("chrome.exe", "--make-default-browser");
            break;
        case "internetexplorer":
           // still can't figure out how to set IE as default...
    }
} 

在Chrome瀏覽器中,它很容易使用命令行。

在Firefox中, -setDefaultBrowser選項不起作用 ,因此我需要為此更改所有注冊表項。 當我在Firefox中使用我的方法后查看“默認程序”時,表明設置了9個默認值中的4個,所以第一個問題是我缺少哪些注冊表項?

對於IE,是那些與Firefox相同的注冊表項,還是還有其他方法? shmgrate.exe OcinstallreinstallIE在Win7上不起作用)

任何幫助,將不勝感激。

有點過分了,但是它可以使用UIAutomation完美地模擬用戶並適合我的情況,這是我的解決方案希望對所有人有幫助:

首先,我在“默認程序”頁面上運行控制面板,然后單擊“設置默認程序”鏈接,然后從程序列表中選擇所需的瀏覽器,然后單擊“設置為默認”按鈕(您將需要嘗試跟隨...):

public static bool SetSystemDefaultBrowserWithGUI(string aBrowserName)
{
    Process.Start("control", "/name Microsoft.DefaultPrograms");
    AutomationElement defaultPrograms = GetSpecificAutomationItem("Default Programs", "Set your default programs");
    var invokePattern = defaultPrograms.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;
    invokePattern.Invoke();

    AutomationElement browserItem = GetSpecificAutomationItem("Set Default Programs", aBrowserName);
    var invokePattern = browserItem.GetCurrentPattern(SelectionItemPattern.Pattern) as SelectionItemPattern;
    invokePattern.AddToSelection();

    AutomationElement setButton = GetSpecificAutomationItem("Set Default Programs", "Set this program as default");
    var invokePattern = setButton.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;
    invokePattern.Invoke();

    WindowPattern wpCloseForm = (WindowPattern)GetSpecificWindow("Set Default Programs").GetCurrentPattern(WindowPattern.Pattern);
    wpCloseForm.Close();
}

當然,這可以使任何程序成為默認程序,這是UIAutomation的一個很好的例子。 順便說一句,這已經在Windows 8上進行了測試,並且也可以使用。

此處使用的功能的附錄

public static AutomationElement GetSpecificWindow(string aWinTitle)
{
    AutomationElement mainWindow = null;
    AutomationElementCollection winCollection = AutomationElement.RootElement.FindAll(TreeScope.Children, Condition.TrueCondition);

    foreach (AutomationElement ele in winCollection)
    {
        if (ele.Current.Name.ToLower() == aWinTitle.ToLower())
        {
            mainWindow = ele;
            break;
        }
    }
    return mainWindow;
}

public static AutomationElement GetSpecificAutomationItem(string aWinTitle, string itemName)
{
    AutomationElement window = GetSpecificWindow(aWinTitle);      
    Condition condition = new PropertyCondition(AutomationElement.NameProperty, itemName);
    return window.FindFirst(TreeScope.Element | TreeScope.Descendants, condition);
}

*當使用Thread.Sleep() try/catch等為自己優化時,所有這些將更好地工作...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM