Stream.CopyTo Method
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Reads the bytes from the current stream and writes them to another stream. Both streams positions are advanced by the number of bytes copied.
CopyTo(Stream) |
Reads the bytes from the current stream and writes them to another stream. Both streams positions are advanced by the number of bytes copied. |
CopyTo(Stream, Int32) |
Reads the bytes from the current stream and writes them to another stream, using a specified buffer size. Both streams positions are advanced by the number of bytes copied. |
- Source:
- Stream.cs
- Source:
- Stream.cs
- Source:
- Stream.cs
Reads the bytes from the current stream and writes them to another stream. Both streams positions are advanced by the number of bytes copied.
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
The stream to which the contents of the current stream will be copied.
Exceptions
destination
is null
.
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.
An I/O error occurred.
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
.NET 9 and other versions
Product | Versions |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1 |
UWP | 10.0 |
- Source:
- Stream.cs
- Source:
- Stream.cs
- Source:
- Stream.cs
Reads the bytes from the current stream and writes them to another stream, using a specified buffer size. Both streams positions are advanced by the number of bytes copied.
public:
void CopyTo(System::IO::Stream ^ destination, int bufferSize);
public:
virtual void CopyTo(System::IO::Stream ^ destination, int bufferSize);
public void CopyTo (System.IO.Stream destination, int bufferSize);
public virtual void CopyTo (System.IO.Stream destination, int bufferSize);
member this.CopyTo : System.IO.Stream * int -> unit
abstract member CopyTo : System.IO.Stream * int -> unit
override this.CopyTo : System.IO.Stream * int -> unit
Public Sub CopyTo (destination As Stream, bufferSize As Integer)
Public Overridable Sub CopyTo (destination As Stream, bufferSize As Integer)
Parameters
- destination
- Stream
The stream to which the contents of the current stream will be copied.
- bufferSize
- Int32
The size of the buffer. This value must be greater than zero. The default size is 81920.
Exceptions
destination
is null
.
bufferSize
is negative or zero.
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.
An I/O error occurred.
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
.NET 9 and other versions
Product | Versions |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1 |
UWP | 10.0 |
.NET feedback
.NET is an open source project. Select a link to provide feedback: