Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open a html file using default web browser

Tags:

browser

c#

I'm using this to get the path and executable of default web browser:

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")

The question is: the above function is calling Firefox and IE (the two web browsers installed on my pc) instead of Internet Explorer, the default web browser. I have no idea how to fix this.

EDIT

I have downloaded and installed the Google Chrome, set it as default web browser, but oddly the above error don't happens with it.

like image 893
Jack Avatar asked Jun 12 '12 02:06

Jack


People also ask

How do I open my HTML file in Google Chrome?

Fire up Chrome and jump to the webpage you want to view the HTML source code. Right-click the page and click on “View Page Source,” or press Ctrl + U, to see the page's source in a new tab. A new tab opens along with all the HTML for the webpage, completely expanded and unformatted.

Why HTML file is not opening in browser?

Check if the file is saved with a UTF-8 encoding. If that doesn't work, try installing another browser or using Edge/Safari/Internet Explorer or whatever built-in browser you have. It is saved as index.

Why is my HTML file not opening in Chrome?

The file at file:///Users/.../index.html is not readable. It may have been removed, moved, or file permissions may be preventing access. Community content may not be verified or up-to-date.


1 Answers

You can replace all that code with

System.Diagnostics.Process.Start(pathToHtmlFile);

This will automatically start your default browser, or rather look up the default handler for .htm or .html files and use that.

Now with Firefox set as default this can sometimes cause weird exceptions (I think if Firefox is starting for first time), so you might want to do a try/catch on it to handle that.

like image 145
Darko Z Avatar answered Oct 04 '22 04:10

Darko Z