Open html file using default web browser

I use this to get the default web browser path and executable:

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; } } } 

and

  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; } 

Then I call the web browser: Run(DefaultWebBrowser, "foo.html")

Question: I have a problem. The above function calls firefox and IE (two web browsers installed on my computer), and not Internet Explorer, the default web browser. I do not know how to fix this. Any help is greatly appreciated. Thanks in advance.

EDIT

I downloaded and installed Google Chrome, installed it as the default web browser, but, oddly enough, the above error does not occur with it.

+6
source share
3 answers

You can replace all this code with

 System.Diagnostics.Process.Start(pathToHtmlFile); 

This will automatically launch your default browser, or rather, look for the default handler for .htm or .html files and use it.

Now that Firefox is installed by default, it can sometimes cause strange exceptions (I think that if Firefox starts for the first time), so you might want to try/catch with it to handle this.

+24
source

For those who do not have a html default association with a browser, use

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

0
source

Im using the code where I first search for exe files. For example, if exsists chrome.exe (in its default path) else, if firefox.exe or launcher.exe (for opera) exists, etc. If it does not exist, try running iexplore.exe with the pathToHtmlFile parameter. This is my solution, where I use the external configuration, where my browser is installed, it does not matter what is installed by default in the OS.

0
source

Source: https://habr.com/ru/post/917838/


All Articles