如何在Windows 7上使用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选项不起作用 ,所以我需要为此目的更改所有registry项。 当我使用我的Firefox方法查看默认程序时,显示9个默认设置中有4个设置了,所以第一个问题是我缺less哪些registry项?

而对于IE浏览器来说,那些与Firefox相同的registry键还是有另一种方式? ( 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等…