19

I would like to know the best way to create a simple html file using c#.

Is it using something like System.IO.File.Create?

tshepang
  • 12,111
  • 21
  • 91
  • 136
Johan
  • 35,120
  • 54
  • 178
  • 293
  • Depends on how you want to provide the data to it. But File.Open, File.Create, File.WriteAllText etc. all work. FileStream, StreamWriter, XmlWriter are also viable choices. Look through the documentation for these classes and methods on MSDN and choose the one most suitable to your needs. – DeCaf Oct 18 '11 at 08:15
  • Johan, what is your problem - creating a file in C# or not knowing what you have to write into it to make a valid HTML? These are two very different questions. – Doc Brown Oct 18 '11 at 08:18
  • @DocBrown Creating the file. Looks like the rest of the guys understood the question :) – Johan Oct 18 '11 at 08:31
  • @DeCaf Technically with XmlWriter you can write only well-formatted XHTML, or only a subset of HTML (the one that doesn't uses unclosed elements and other things) – xanatos Oct 18 '11 at 08:41
  • @xanatos: That is true of course. – DeCaf Oct 18 '11 at 08:42

8 Answers8

35

Something like -

using (FileStream fs = new FileStream("test.htm", FileMode.Create)) 
{ 
    using (StreamWriter w = new StreamWriter(fs, Encoding.UTF8)) 
    { 
        w.WriteLine("<H1>Hello</H1>"); 
    } 
} 
ipr101
  • 24,096
  • 8
  • 59
  • 61
  • Is there any smart way to choose a path? Like a "save as" button or something similar – Johan Oct 18 '11 at 08:32
  • @Johan - If you're using Windows Forms you could try this - http://msdn.microsoft.com/en-us/library/system.windows.forms.folderbrowserdialog.aspx – ipr101 Oct 18 '11 at 08:38
  • I dont think i can use System.Windows. Are there any other options? – Johan Oct 18 '11 at 08:44
  • What are you using? Web forms, WPF etc – ipr101 Oct 18 '11 at 08:52
  • Its webforms. Im using this code atm: http://zamov.online.fr/EXHTML/CSharp/CSharp_302155.html But how do i specify it to browse on my local machine? Its running on a IIS webserver atm – Johan Oct 18 '11 at 09:00
  • If you're using web forms you won't be able to create a file on your local machine, you'd need to create the file on the server then send it to the client. – ipr101 Oct 18 '11 at 09:14
  • Ok, but how do i browse the local machine instead of the server? – Johan Oct 18 '11 at 10:01
8

I'll say that File.WriteAllText is a stupid-proof way to write a text file for C# >= 3.5.

File.WriteAllText("myfile.htm", @"<html><body>Hello World</body></html>");

I'll even say that File.WriteAllLines is stupid-proof enough to write bigger html without fighting too much with string composition. But the "good" version is only for C# 4.0 (a little worse version is C# >= 2.0)

List<string> lines = new List<string>();
lines.Add("<html>");
lines.Add("<body>");
lines.Add("Hello World");
lines.Add("</body>");
lines.Add("</html>");

File.WriteAllLines("myfile.htm", lines);
// With C# 3.5
File.WriteAllLines("myfile.htm", lines.ToArray());
RyanfaeScotland
  • 1,216
  • 11
  • 30
xanatos
  • 109,618
  • 12
  • 197
  • 280
4

I would go with File.Create and then open a StreamWriter to that file if you dont have all the data when you create the file. This is a example from MS that may help you

class Test 
{
    public static void Main() 
    {
        string path = @"c:\temp\MyTest.txt";

        // Create the file.
        using (FileStream fs = File.Create(path, 1024)) 
        {
            Byte[] info = new UTF8Encoding(true).GetBytes("This is some text in the file.");
            // Add some information to the file.
            fs.Write(info, 0, info.Length);
        }

        // Open the stream and read it back.
        using (StreamReader sr = File.OpenText(path)) 
        {
            string s = "";
            while ((s = sr.ReadLine()) != null) 
            {
                Console.WriteLine(s);
            }
        }
    }
}
Marthin
  • 6,413
  • 15
  • 58
  • 95
2

Have a look at the HtmlTextWriter class. For an example how to use this class, for example look at http://www.dotnetperls.com/htmltextwriter.

Nubok
  • 3,502
  • 7
  • 27
  • 47
1

Reading and writing text files and MSDN info. HTML is just a simple text file with *.HTML extension ;)

mike
  • 550
  • 2
  • 9
  • 24
0

Simply opening a file for writing (using File.OpenWrite() for example) will create the file if it does not yet exist.

Sjoerd
  • 74,049
  • 16
  • 131
  • 175
0

If you have a look at http://msdn.microsoft.com/en-us/library/d62kzs03.aspx you can find an example of creating a file.

But how do you want to create the html file content? If that's just static then you can just write it to a file.. if you have to create the html on the fly you could use an ASPX file with the correct markup and use a Server.Execute to get the HTML as a string.

Wouter de Kort
  • 39,090
  • 12
  • 84
  • 103
0

Yep, System.IO.File.Create(Path) will create your file just fine. You can also use a filestream and write to it. Seems more handy to write a htm file

Frederiek
  • 1,605
  • 2
  • 17
  • 32