23

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.

6 Answers 6

49

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.

Sign up to request clarification or add additional context in comments.

7 Comments

I had tried it. But in some pcs .htm/.html are not opened with a web browser. the .htm/.html extensions can be associated with an text editor or IDE for example.
Although the default program can be changed, you shouldn't have a problem really. See this for some tips on using ShellExecute to launch the default web browser, and also the paths for some registry keys (which you're probably already aware of). In the end, the user can most probably intervene by changing some default programs... but you shouldn't worry too much about this, as it's unavoidable to a certain extent.
As Jack said, this is a terrible idea. I personally have an editor set as default application to open HTML files, and it unnerves me to no end to see programs open their readme file in that editor instead of the default browser. There IS a difference between 'default browser' and 'default app to open HTML with' in Windows.
This should not be the accepted answer. Does not guarantee that the file will be opened with a browser.
@Theyouthis feel free to contribute if you have a better solution
|
29

UPDATE 30 April 2022: For .Net Core better solution is to set UseShellExecute = true as suggested in .Net Core 2.0 Process.Start throws "The specified executable is not a valid application for this OS platform"

Original suggestion: For .Net Core you need to call (suggested in .Net Core 2.0 Process.Start throws "The specified executable is not a valid application for this OS platform")

 var proc = Process.Start(@"cmd.exe ", @"/c " + pathToHtmlFile); 

When I tried Process.Start(pathToHtmlFile);, I've got System.ComponentModel.Win32Exception: The specified executable is not a valid application for this OS platform.

1 Comment

This would be the preferred way, actually.
11

In case if you're getting System.ComponentModel.Win32Exception exception, you need to set UseShellExecute to true

var p = new Process();
p.StartInfo = new ProcessStartInfo(@"C:\path\test.html")
{
    UseShellExecute = true
};
p.Start();

See .Net Core 2.0 Process.Start throws "The specified executable is not a valid application for this OS platform"

2 Comments

this is the only one that works for me in .net core 8, others (proc = Process.Start(@"cmd.exe ", @"/c " + pathToHtmlFile); , etc) do not work anymore
this needs to be the apex answer
1

For those who don't have html default association to a browser, use

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

6 Comments

It assumes that the user has chrome browser installed.
The code for DefaultWebBrowser is given at the top of the page
Back to using that code if you want it more generic (sorry assumed it was obvious).
Ok, I see your original problem was something funky going on with DefaulWebBrowser. - "the above function is calling the firefox and IE".
didn't work, even though I have chrome installed (as default)
|
0

I got this error when I used System.Diagnostics.Process.Start(pathToHtmlFile); :

System.ComponentModel.Win32Exception: 'The specified executable is not a valid application for this OS platform.'

I was able to open the html file in the default browser with this code :

System.Diagnostics.Process process = new System.Diagnostics.Process();
try
{
    process.StartInfo.UseShellExecute = true;
    process.StartInfo.FileName = pathToHtmlFile;
    process.Start();
}
catch (Exception e)
{
    Console.WriteLine(e.Message);
}

Comments

-1

Im using a code, where i look up for exe files first. For example, if exsists chrome.exe (in its default path) else if exists firefox.exe or launcher.exe (for opera) etc... if doesnt any exists, try to run iexplore.exe with pathToHtmlFile parameter. This is my solution, where i use external config, where a set my browser, doesnt matter what is set default in OS.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.