21

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

44

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.

5
  • 3
    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.
    – Jack
    Jun 12, 2012 at 2:10
  • 1
    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.
    – Spooky
    Jun 12, 2012 at 4:37
  • 1
    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.
    – Nyerguds
    Dec 2, 2013 at 14:55
  • 1
    This should not be the accepted answer. Does not guarantee that the file will be opened with a browser. Oct 19, 2015 at 20:27
  • @Theyouthis feel free to contribute if you have a better solution
    – Darko
    Oct 20, 2015 at 3:04
21

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
  • 2
    This would be the preferred way, actually.
    – Marc.2377
    Jul 22, 2021 at 3:18
4

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"

1

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

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

6
  • It assumes that the user has chrome browser installed.
    – Jack
    Jan 23, 2017 at 18:24
  • The code for DefaultWebBrowser is given at the top of the page Jan 24, 2017 at 4:49
  • Back to using that code if you want it more generic (sorry assumed it was obvious). Jan 24, 2017 at 4:55
  • Ok, I see your original problem was something funky going on with DefaulWebBrowser. - "the above function is calling the firefox and IE". Jan 24, 2017 at 5:01
  • 2
    didn't work, even though I have chrome installed (as default)
    – z0mbi3
    Sep 12, 2017 at 16:03
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);
}
-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.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.