我需要编写一些带有样式(如颜色、字体)的文本,所以我决定使用 html。我发现这HtmlTextWriter
是一个用于编写 html 文件的类。但是,我发现我必须手动关闭或刷新它,否则不会将任何内容写入文件。为什么?(使用语句应该在块完成时处理它)
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
提前致谢。