如何在.NET Framework 3.5的使用Stream.CopyTo? [英] How to use Stream.CopyTo on .NET Framework 3.5?

查看:585
本文介绍了如何在.NET Framework 3.5的使用Stream.CopyTo?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现上传code和这code包含 Stream.CopyTo 方法。

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

例如:

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

我如何可以复制file.Stream到requestStream?

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

推荐答案

您不能,基本上是这样。这只是在.NET 4实现可以自己写,虽然类似的方法...甚至使之扩展方法:

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

这篇关于如何在.NET Framework 3.5的使用Stream.CopyTo?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆