Open a html file using default web browser

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.


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.


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

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


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"

Tags:

C#

Browser