CopyTo Method (Stream)
Collapse the table of content
Expand the table of content

Stream.CopyTo Method (Stream)

.NET Framework (current version)
 

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

Namespace:   System.IO
Assembly:  mscorlib (in mscorlib.dll)

public void CopyTo(
	Stream destination
)

Parameters

destination
Type: System.IO.Stream

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

Exception Condition
ArgumentNullException

destination is null.

NotSupportedException

The current stream does not support reading.

-or-

destination does not support writing.

ObjectDisposedException

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

IOException

An I/O error occurred.

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.

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

Universal Windows Platform
Available since 4.5
.NET Framework
Available since 4.0
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 4.0
Windows Phone Silverlight
Available since 7.1
Windows Phone
Available since 8.1
Return to top
Show:
© 2016 Microsoft