A
Alexander Muylaert
Hi
for some reason the BinaryWriter is Closing the stream when disposed. I
don't want it to close the stream. According to my logic the BinaryWriter
should Write to the stream, nothing more. If I want the stream to close, I
will close it myself, not some helper class.
According to me the BinaryWriter should *not* touch the stream. Except for
the expected behaviour of incrementing the Position of the stream and
writing data to it.
Imagine this ...
private void WriteWhateverBinaryThingsToStream(Stream aStream){
using (BinaryWriter Writer = New BinaryWriter(aStream)){
Writer.WriteInt64(MyInt64);
Writer.WriteString(MyString);
}
}
public void SaveToFile(string FileName){
using (FileStream fs = new FileStream("c:\\test.dat", FileMode.Create))
{
WriteWhateverBinaryThingsToStream(fs);
WriteWhateverBinaryThingsToStream(fs); // stream is already close
here.
WriteWhateverBinaryThingsToStream(fs);
}
}
Even when I don't use the "using" keyword I cannot create multiple instances
of BinaryWriters that work on the same stream. Because GC can close the
stream at any time. Please tell me I'm doing something very, very wrong
here. If my assumption is correct, could somebody explain the advantages to
me. I see none .
Kind regards and many thanks
Alexander
for some reason the BinaryWriter is Closing the stream when disposed. I
don't want it to close the stream. According to my logic the BinaryWriter
should Write to the stream, nothing more. If I want the stream to close, I
will close it myself, not some helper class.
According to me the BinaryWriter should *not* touch the stream. Except for
the expected behaviour of incrementing the Position of the stream and
writing data to it.
Imagine this ...
private void WriteWhateverBinaryThingsToStream(Stream aStream){
using (BinaryWriter Writer = New BinaryWriter(aStream)){
Writer.WriteInt64(MyInt64);
Writer.WriteString(MyString);
}
}
public void SaveToFile(string FileName){
using (FileStream fs = new FileStream("c:\\test.dat", FileMode.Create))
{
WriteWhateverBinaryThingsToStream(fs);
WriteWhateverBinaryThingsToStream(fs); // stream is already close
here.
WriteWhateverBinaryThingsToStream(fs);
}
}
Even when I don't use the "using" keyword I cannot create multiple instances
of BinaryWriters that work on the same stream. Because GC can close the
stream at any time. Please tell me I'm doing something very, very wrong
here. If my assumption is correct, could somebody explain the advantages to
me. I see none .
Kind regards and many thanks
Alexander