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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Jack
  • 16,276
  • 55
  • 159
  • 284

6 Answers6

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.

Darko
  • 38,310
  • 15
  • 80
  • 107
  • 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 '12 at 02:10
  • 1
    Although the default program _can_ be changed, you shouldn't have a problem really. See [this](http://support.microsoft.com/kb/224816) 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 '12 at 04: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 02 '13 at 14:55
  • 1
    This should not be the accepted answer. Does not guarantee that the file will be opened with a browser. – BillHaggerty Oct 19 '15 at 20:27
  • @Theyouthis feel free to contribute if you have a better solution – Darko Oct 20 '15 at 03:04
22

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.

Michael Freidgeim
  • 26,542
  • 16
  • 152
  • 170
5

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"

Michael Freidgeim
  • 26,542
  • 16
  • 152
  • 170
Berz
  • 63
  • 2
  • 4
1

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

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

Richard Matsen
  • 20,671
  • 3
  • 43
  • 77
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);
}
utarid
  • 1,642
  • 4
  • 24
  • 38
-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.