Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Stream.CopyTo on .NET Framework 3.5?

Tags:

c#

I found upload code and this code contains the Stream.CopyTo method.

Example:

  file.Stream.CopyTo(requestStream); // .NET Framework 4.0

How can I copy "file.Stream" to "requestStream"?

like image 584
Mennan Avatar asked Apr 20 '11 13:04

Mennan


1 Answers

You can't, basically. It's only implemented in .NET 4. You can write a similar method yourself though... and even make it an extension method:

// Only useful before .NET 4
public static void CopyTo(this Stream input, Stream output)
{
    byte[] buffer = new byte[16 * 1024]; // Fairly arbitrary size
    int bytesRead;

    while ((bytesRead = input.Read(buffer, 0, buffer.Length)) > 0)
    {
        output.Write(buffer, 0, bytesRead);
    }
}
like image 167
Jon Skeet Avatar answered Oct 24 '22 09:10

Jon Skeet