Library code snippets
Save a Stream to a File
By James Crowley, published on 13 Feb 2005
If you've got a Stream in .NET - whether its fetched from a HttpWebRequest or read from another file - you can easily save this stream to another file using the following code.
// readStream is the stream you need to read
// writeStream is the stream you want to write to
private void ReadWriteStream(Stream readStream, Stream writeStream)
{
int Length = 256;
Byte [] buffer = new Byte[Length];
int bytesRead = readStream.Read(buffer,0,Length);
// write the required bytes
while( bytesRead > 0 )
{
writeStream.Write(buffer,0,bytesRead);
bytesRead = readStream.Read(buffer,0,Length);
}
readStream.Close();
writeStream.Close();
}
To call this method, just do something like this:
string saveTo = "some path to save"
// create a write stream
FileStream writeStream = new FileStream(saveTo, FileMode.Create, FileAccess.Write);
// write to the stream
ReadWriteStream(readStream,writeStream);
Related articles
Related discussion
-
Installing windows service using MSI
by chirpyanu (0 replies)
-
Class, Abstract or Interface ?
by uzay95 (0 replies)
-
C++ dll for USB download in C# application
by sumahs (0 replies)
-
a bit of an abstract question about Test Driven Development
by gtejeda (0 replies)
-
An Introduction to VB.NET and Database Programming
by bitkisel (13 replies)
Related podcasts
-
Looking into the C# Crystal Ball with Charlie Calvert and Bill Wagner
One of the most exciting announcements from PDC was the news about C# 4.0 and Visual Studio 2010. With all the excitement and discussion throughout the event about these new developer tools, we reached out to two experts in the fields. Charlie Calvert and Bill Wagner sat down with Keith and Woody...
Events coming up
-
Jun
16
Code Generation 2009
Cambridge, United Kingdom
A developer event with a practical focus on helping people get to grips with code generation tools and technologies.
Thank's a lot from Mexico City
This thread is for discussions of Save a Stream to a File.