I need to write some text with style (like color, fonts) so I decided to use html. I found that HtmlTextWriter
is a class used for writing html file. However, I found that I must manually close or flush it otherwise nothing is written to the file. Why is it? (using statement should dispose it when the block is finished)
using (HtmlTextWriter htmlWriter = new HtmlTextWriter(new StreamWriter(
Path.Combine(EmotionWordCounts.FileLocations.InputDirectory.FullName, fileName),
false, Encoding.UTF8)))
{
try
{
htmlWriter.WriteFullBeginTag("html");
htmlWriter.WriteLine();
htmlWriter.Indent++;
htmlWriter.WriteFullBeginTag("body");
htmlWriter.WriteLine();
htmlWriter.Indent++;
// write something using WriteFullBeginTag and WriteEndTag
// ...
} //try
finally
{
htmlWriter.Indent--;
htmlWriter.WriteEndTag("body");
htmlWriter.WriteLine();
htmlWriter.Indent--;
htmlWriter.WriteEndTag("html");
htmlWriter.Close(); // without this, the writer doesn't flush
}
} //using htmlwriter
Thanks in advance.