One great new feature in .NET 4.0 is the Stream.CopyTo method. In order to copy a stream in .NET 3.5 we needed to use something like:
public static long CopyStream(Stream source, Stream target)
{
const int bufSize = 0x1000;
byte[] buf = new byte[bufSize];
long totalBytes = 0;
int bytesRead = 0;
while ((bytesRead = source.Read(buf, 0, bufSize)) > 0)
{
target.Write(buf, 0, bytesRead);
totalBytes += bytesRead;
}
return totalBytes;
}
Now we can simply use source.CopyTo(destination). One of the many little improvements .NET 4.0 has to offer in the base classes.
Posted
27 Nov 2009 8:50 AM
by
Gal Ratner