使用默认Web浏览器打开html文件

我正在使用它来获取默认Web浏览器的路径和可执行文件:

public static string DefaultWebBrowser { get { string path = @"\http\shell\open\command"; using (RegistryKey reg = Registry.ClassesRoot.OpenSubKey(path)) { if (reg != null) { string webBrowserPath = reg.GetValue(String.Empty) as string; if (!String.IsNullOrEmpty(webBrowserPath)) { if (webBrowserPath.First() == '"') { return webBrowserPath.Split('"')[1]; } return webBrowserPath.Split(' ')[0]; } } return null; } } } 

和:

  protected static bool Run(string FileName, string Args) { try { Process proc = new Process(); processInfo.FileName = FileName; proc.StartInfo.WindowStyle = ProcessWindowStyle.Normal; if(Args != null) proc.StartInfo.Arguments = Args; proc.Start(); return true; } catch (Exception) { } return false; } 

然后我调用Web浏览器: Run(DefaultWebBrowser, "foo.html")

问题是:上面的function是调用Firefox和IE(我的电脑上安装的两个Web浏览器)而不是Internet Explorer,即默认的Web浏览器。 我不知道如何解决这个问题。

编辑

我已经下载并安装了谷歌浏览器,将其设置为默认的网络浏览器,但奇怪的是上述错误不会发生。

您可以用所有代码替换

 System.Diagnostics.Process.Start(pathToHtmlFile); 

这将自动启动您的默认浏览器,或者查找.htm.html文件的默认处理程序并使用它。

现在将Firefox设置为默认值,这有时会导致奇怪的exception(我想如果Firefox首次启动),所以你可能想对它进行try/catch来处理它。

对于那些没有html默认关联到浏览器的人,请使用

System.Diagnostics.Process.Start("Chrome", Uri.EscapeDataString(pathToHtmlFile))

我正在使用代码,我首先查找exe文件。 例如,如果exsists chrome.exe(在其默认路径中),否则如果存在firefox.exe或launcher.exe(用于opera)等…如果不存在,请尝试使用pathToHtmlFile参数运行iexplore.exe。 这是我的解决方案,我使用外部配置,其中设置我的浏览器,无关紧要在操作系统中设置默认值。