How to save stream data to file using filestream?

T

terrorix

Hi,

I have function which return instance of object Stream and i want that stream object to save to file.
How can i do that?

my code is:

Stream s = MyGetStream();

my question is what i must to do to save stream object s to file ?
 
J

Joerg Jooss

terrorix said:
Hi,

I have function which return instance of object Stream and i want
that stream object to save to file. How can i do that?

my code is:

Stream s = MyGetStream();

my question is what i must to do to save stream object s to file ?

The quintessential stream copying method:

public void Copy(Stream source, Stream target) {
byte[] buffer = new byte[0x10000];
int bytes;
try {
while ((bytes = source.Read(buffer, 0, buffer.Length)) > 0) {
target.Write(buffer, 0, bytes);
}
}
finally {
target.Flush();
// Or target.Close(); if you're done here already.
}
}

If you use a FileStream instance as "target", you'll get what you want.

Cheers,
 

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

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top