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

后端 未结 1 873
难免孤独
难免孤独 2020-12-03 09:45

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

Example:

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


        
相关标签:
1条回答
  • 2020-12-03 10:17

    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);
        }
    }
    
    0 讨论(0)
提交回复
热议问题