Problem With TextWriter

K

kengtung

Greetings all,

I just started programming with C# (.Net FW 1.1) last year and still
learning. Previously I am using VB 6.0.

Recently, in one application, I encountered a C# error "Cannot write
to a closed TextWriter.". I looked at the code and it looks ok to me.
Maybe I am missing some understanding here on the inner function of
TextWriter. Any information is much appreciated.

Another question is should I close both textWriter and streamWriter or
just the streamWriter? I just want to make sure there is no stream
objects left in the memory when GC runs.

Code snippets:

[STAThread]
static void Main(string[] args)
{
string filePath = Directory.GetCurrentDirectory() + "\\test.htm";
StreamWriter streamWriter = null;
if( File.Exists( filePath ) )
streamWriter = new StreamWriter( filePath );
else
streamWriter = File.CreateText( filePath );

XmlTextWriter textWriter = new XmlTextWriter( streamWriter );
try
{
textWriter.WriteStartElement("start");
textWriter.WriteElementString("data", "123");
textWriter.WriteEndElement();
}
finally
{
if (textWriter != null)
{
textWriter.Flush();
textWriter.Close();
textWriter = null;
}
if (streamWriter != null)
{
streamWriter.Flush();
streamWriter.Close();

streamWriter = null;
}
}
}

<EOC>

Many thanks in advance.
KT Lee.
 
P

Peter Duniho

Recently, in one application, I encountered a C# error "Cannot write
to a closed TextWriter.". I looked at the code and it looks ok to me.
Maybe I am missing some understanding here on the inner function of
TextWriter. Any information is much appreciated.

While you don't actually say which line of code causes the error (you
should have, by the way), I suspect it's your call to
StreamWriter.Flush() in the "finally" block.

When you close the XmlTextWriter, that also closes the underlying
stream. Flushing a stream will attempt to write the stream, but since
you've already closed the XmlTextWriter, the attempt to call Flush()
fails with that exception.
Another question is should I close both textWriter and streamWriter or
just the streamWriter? I just want to make sure there is no stream
objects left in the memory when GC runs.

See above.

Also, I don't see the point in checking for file existence. The
File.CreateText() method specific allows you to ignore the question. If
the file exists, it will open it for writing, and if it doesn't, it will
create it and then open it.

Pete
 
S

Samuel R. Neff

textWriter wraps streamWriter, they both refer to the same stream, so
when you close textWriter, you're closing streamWriter as well.

besides that, your code can be nicely abbreviated..

using(
XmlTextWriter writer = new XmlTextWriter(
new StreamWriter(
Path.Combine(Directory.GetCurrentDirectory(), "test.htm")) {

writer.WriteStartElement("start");
writer.WriteElementString("data", "123");
writer.WriteEndElement();
}

HTH,

Sam
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top