It's fairly simple to provide your own version of the
CopyTo
method:
C#:
using System;
using System.IO;
public static class StreamExtensions
{
public static void CopyTo(this Stream source, Stream destination)
{
if (source == null) throw new ArgumentNullException("source");
if (destination == null) throw new ArgumentNullException("destination");
if (!source.CanRead) throw new NotSupportedException("Cannot read from the source stream.");
if (!destination.CanWrite) throw new NotSupportedException("Cannot write to the destination stream.");
int bytesRead;
byte[] buffer = new byte[81920];
while ((bytesRead = source.Read(buffer, 0, buffer.Length)) != 0)
{
destination.Write(buffer, 0, bytesRead);
}
}
}
VB.NET:
Imports System.IO
Imports System.Runtime.CompilerServices
Public Module StreamExtensions
<Extension> _
Public Sub CopyTo(ByVal source As Stream, ByVal destination As Stream)
If source Is Nothing Then
Throw New ArgumentNullException("source")
End If
If destination Is Nothing Then
Throw New ArgumentNullException("destination")
End If
If Not source.CanRead Then
Throw New NotSupportedException("Cannot read from the source stream.")
End If
If Not destination.CanWrite Then
Throw New NotSupportedException("Cannot write to the destination stream.")
End If
Dim buffer As Byte() = New Byte(81919) {}
Dim bytesRead As Integer = source.Read(buffer, 0, buffer.Length)
While bytesRead <> 0
destination.Write(buffer, 0, bytesRead)
bytesRead = source.Read(buffer, 0, buffer.Length)
End While
End Sub
End Module