简体   繁体   中英

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

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"?

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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM