It greatly depends on what the stream is. More information would be helpful. The snippet below - depending on whether or not the stream supports seeking - would work for many cases (but certainly not all, like with a NetworkStream - but not limited to it):
if (stream.CanSeek<)
{
stream.Seek(0, SeekOrigin.Begin);
using (FileStream file = new FileStream("file.dat", FileMode.Create,
FileAccess.Write, FileShare.Read))
{
int read = 0;
byte[] buffer = new byte[4096];
while ((read = stream.Read(buffer, 0, 4096)) > 0)
{
file.Write(buffer, 0, read);
}
file.Flush();
}
}
This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles]