System.io.Stream 不包含 CopyTo()的定义的原因

 

因为Stream.CopyTo是 .net 4.0 引入的。 所以 .net 2.0, vs 2005 中,是找不到这个的 

 

 

 

 

 

Stream.CopyTo 是在 .NET 4 中引入的。由于您的目标是 .Net 2.0,因此它不可用。 在内部, CopyTo主要执行此操作(尽管有额外的错误处理),因此您可以使用此方法。 为方便起见,我将其作为扩展方法。

//it seems 81920 is the default size in CopyTo but this can be changed
public static void CopyTo(this Stream source, Stream destination, int bufferSize = 81920)
{
    byte[] array = new byte[bufferSize];
    int count;
    while ((count = source.Read(array, 0, array.Length)) != 0)
    {
       destination.Write(array, 0, count);
    }
}

 

posted @ 2021-08-23 13:54  Hans.NET  阅读(223)  评论(0编辑  收藏  举报