Launch Browser from code with Generated HTML in C#

There is a quick way to have a companion browser come up with generated html from your application in C# and .Net. The following example simulates getting or generating the html, saving it to a file and then launching the file from the command line.

First off you will need to include these namespaces:
using System.Diagnostics;
using System.IO;
Then you can do this code.
string filename
    = string.Format(@"{0}\{1}",

File.WriteAllText(filename, "<h1>Hello</h1>");

  • Line 02: We assume the user does not have the final slash. A more diligent programmer will check for the slash.
  • Line 06: We simply write out the html to a file.
  • Line 08: Thanks to the OS it has assigned the extension to a launchable application and htm usually goes with the default browser. By calling the static Start on the Process Class we launch the html file.
Don’t forget to delete (or attempt to delete the file) when your program closes as a courtesy.


  1. virtualblackfox says:

    Hi, you could also use

    string filename = System.IO.Path.Combine(System.IO.Path.GetTempPath(), “testhtm.htm”);

    so you could ignore if the user put the slash or not.

  2. omegaman says:

    Excellent suggestion!

  3. Yohann says:

    Didn’t know File.WriteAllText() existed. Neat! No need to open a Stream and a StreamWriter.


  4. Larisa says:

    I am a beginner in .NET. Thank you!! It’s best solution

Leave a Reply