如何使用C#来设置默认浏览器在Windows 7? [英] How to set the default browser on Windows 7 using c#?

查看:217
本文介绍了如何使用C#来设置默认浏览器在Windows 7?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个接收浏览器名称和更改系统默认的主流浏览器之一的方法:

I'm writing a method that receives a browser name and changes the system default to one of the major browsers:

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浏览器很容易,用命令行工作。

In Chrome it's easy and working with command line.

在Firefox中有这么的 -setDefaultBrowser 选项=htt​​ps://support.mozilla.org/en-US/questions/960677相对=nofollow >不工作的,所以我需要改变所有注册表项用于这一目的。当我看到在默认程序使用我的方法对Firefox之后,它表明,4出9默认值分别设置,所以第一个问题就是注册表项我缺少什么?

In Firefox there's -setDefaultBrowser option that doesn't work, so I need to change all registry keys for that purpose. When I look in Default Programs after using my method for Firefox it shows that 4 out of 9 defaults were set, so First question is what registry keys am I missing?

和IE浏览器,是那些相同的注册表项Firefox或有另一种方式? (<一href=\"http://superuser.com/questions/300770/set-internet-explorer-as-the-default-browser-from-the-command-line\"><$c$c>shmgrate.exe OcinstallreinstallIE 不会对Win7的工作)

And for IE, are those the same registry keys as Firefox or is there another way? (shmgrate.exe OcinstallreinstallIE doesn't work on Win7)

任何帮助将是AP preciated。

Any help would be appreciated.

推荐答案

这是一个有点矫枉过正,但它与UIAutomation完全模拟用户和适合我的情况下,这里是我的解决方案,希望它可以帮助别人:

It's a bit of an overkill but it simulates user perfectly with UIAutomation and suits my case, here's my solution hopes it helps anybody:

首先,我运行控制面板中默认程序页面,然后,点击设置默认程序链接,然后选择浏览器从程序列表想,然后点击设置为默认值按钮(你需要尝试自己遵循...):

First, I'm running control panel in 'Default Programs' page, then, clicking the 'Set default programs' link, and then, selecting the browser wanted from the programs list and clicking the 'Set as default' button (You'll need to try yourself to follow...):

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测试和工程了。

Of course this could be used to make any program the default and it is a good example to UIAutomation. BTW, this was tested on Windows 8 and works too.

附录这里使用的功能:

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代码() 尝试/优化,这一切都将更好地工作,抓等...

这篇关于如何使用C#来设置默认浏览器在Windows 7?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆