Stream.CopyTo Stream.CopyTo Stream.CopyTo Stream.CopyTo Method

Definition

Reads the bytes from the current stream and writes them to another stream.

Overloads

CopyTo(Stream, Int32) CopyTo(Stream, Int32) CopyTo(Stream, Int32) CopyTo(Stream, Int32)

Reads the bytes from the current stream and writes them to another stream, using a specified buffer size.

CopyTo(Stream) CopyTo(Stream) CopyTo(Stream) CopyTo(Stream)

Reads the bytes from the current stream and writes them to another stream.

CopyTo(Stream, Int32) CopyTo(Stream, Int32) CopyTo(Stream, Int32) CopyTo(Stream, Int32)

Reads the bytes from the current stream and writes them to another stream, using a specified buffer size.

public:
 void CopyTo(System::IO::Stream ^ destination, int bufferSize);
public void CopyTo (System.IO.Stream destination, int bufferSize);
member this.CopyTo : System.IO.Stream * int -> unit
Public Sub CopyTo (destination As Stream, bufferSize As Integer)
Parameters
destination
Stream Stream Stream Stream

The stream to which the contents of the current stream will be copied.

bufferSize
Int32 Int32 Int32 Int32

The size of the buffer. This value must be greater than zero. The default size is 81920.

Exceptions

The current stream does not support reading.

-or-

destination does not support writing.

Either the current stream or destination were closed before the CopyTo(Stream) method was called.

Remarks

Copying begins at the current position in the current stream, and does not reset the position of the destination stream after the copy operation is complete.

CopyTo(Stream) CopyTo(Stream) CopyTo(Stream) CopyTo(Stream)

Reads the bytes from the current stream and writes them to another stream.

public:
 void CopyTo(System::IO::Stream ^ destination);
public void CopyTo (System.IO.Stream destination);
member this.CopyTo : System.IO.Stream -> unit
Public Sub CopyTo (destination As Stream)
Parameters
destination
Stream Stream Stream Stream

The stream to which the contents of the current stream will be copied.

Exceptions

The current stream does not support reading.

-or-

destination does not support writing.

Either the current stream or destination were closed before the CopyTo(Stream) method was called.

Examples

The following example copies the contents of a FileStream to a MemoryStream.

// Create the streams.
MemoryStream destination = new MemoryStream();

using (FileStream source = File.Open(@"c:\temp\data.dat",
    FileMode.Open))
{

    Console.WriteLine("Source length: {0}", source.Length.ToString());

    // Copy source to destination.
    source.CopyTo(destination);
}

Console.WriteLine("Destination length: {0}", destination.Length.ToString());
' Create the streams.
Dim destination As New MemoryStream()

Using source As FileStream = File.Open("c:\temp\data.dat", _
                                       FileMode.Open)
    Console.WriteLine("Source length: {0}", source.Length.ToString())

    ' Copy source to destination.
    source.CopyTo(destination)

End Using
Console.WriteLine("Destination length: {0}", destination.Length.ToString())

Remarks

Copying begins at the current position in the current stream, and does not reset the position of the destination stream after the copy operation is complete.

Applies to