truncating / resizing stream

A

andrewcw

Should be simple, I open fileX,
then serialize my content back out ( with position set to
0 ). This works well as long as my new output is larger
than the old file input.

But when my output is smaller, it does not overwrite all
the data and so I get the new output plus the overhanging
piece of FileX.

What the quick way to let resize the stream to just my new
output ? THANKS

code to write out object like this:
stream.Position=0;
serializer.Serialize(stream,obj);
System.IO.StreamWriter Tout = new System.IO.StreamWriter
(stream);
stream.Position=0;
Tout.Close();
 
S

Sherif ElMetainy

Hello

you can either use stream.SetLength() method, or if it is a FileStream, open
with FileMode.Create to overrite any exiting file

Best Regards
Sherif
 

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