Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6096873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:58:39+00:00 2026-05-23T12:58:39+00:00

I need to write some text with style (like color, fonts) so I decided

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T12:58:40+00:00Added an answer on May 23, 2026 at 12:58 pm

    This is a bug in HtmlTextWriter. You should make a self-contained test case and report it using Microsoft Connect. It seems that Close and Dispose behave differently, which isn’t documented and is extremely unusual. I also can’t find any documentation on MSDN stating whether the HtmlTextWriter takes ownership of the underlying textwriter or not; i.e. will it dispose the underlying textwriter or must you?

    Edit 2: The MSDN page on HtmlTextWriter states that it inherits (as opposed to overrides) the virtual Dispose(bool) method. This means the current implementation clearly cannot clean up with a using block. As a workaround, try this:

    using(var writer = ...make TextWriter...) 
    using(var htmlWriter = new HtmlTextWriter(writer)) {
    
        //use htmlWriter here...
    
    } //this should flush the underlying writer AND the HtmlTextWriter
    
    // although there's currently no need to dispose HtmlTextWriter since
    // that doesn't do anything; it's possibly better to do so anyhow in 
    // case the implementation gets fixed
    

    Incidentally, new StreamWriter(XYZ, false, Encoding.UTF8) is equivalent to new StreamWriter(XYZ). StreamWriter creates rather than appends by default, and it uses UTF8 without BOM by default as well.

    Good luck – and don’t forget to report the bug!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to create a function in JavaScript, that will write some text (which
I need to write a simple terminal-based program that should, Read some text from
HI. I need to write some text over a custom button. So I subclassed
I need to write some Prolog programs for a class. Any recommendations?
I need to write some sql that will allow me to query all objects
I need to write some code that would loop though all rows of a
I'm beginner and write some HTML code but what I need is when I
I need to write some fixed-width font (i.e Courier New or Consolas) text to
I need to write some client-side javascript for a webpage that will truncate the
I need some help about JavaScript on iPhone UIWebView ; I have HTML like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.