Stream.CopyTo Method

Definition

从当前流中读取字节并将其写入到另一流中。Reads the bytes from the current stream and writes them to another stream.

Overloads

CopyTo(Stream)

从当前流中读取字节并将其写入到另一流中。Reads the bytes from the current stream and writes them to another stream.

CopyTo(Stream, Int32)

使用指定的缓冲区大小,从当前流中读取字节并将其写入到另一流中。Reads the bytes from the current stream and writes them to another stream, using a specified buffer size.

CopyTo(ReadOnlySpanAction<Byte,Object>, Object, Int32)

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

当前流的内容将复制到的流。The stream to which the contents of the current stream will be copied.

Exceptions

destinationnulldestination is null.

当前流不支持读取。The current stream does not support reading.

- 或 --or- destination 不支持写入。destination does not support writing.

在调用 CopyTo(Stream) 方法前当前流或 destination 已关闭。Either the current stream or destination were closed before the CopyTo(Stream) method was called.

出现 I/O 错误。An I/O error occurred.

Examples

下面的示例将 FileStream 的内容复制到 MemoryStreamThe 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.

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:
 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. 默认大小为 81920。The default size is 81920.

Exceptions

destinationnulldestination is null.

bufferSize 为负数或零。bufferSize is negative or zero.

当前流不支持读取。The current stream does not support reading.

- 或 --or- destination 不支持写入。destination does not support writing.

在调用 CopyTo(Stream) 方法前当前流或 destination 已关闭。Either the current stream or destination were closed before the CopyTo(Stream) method was called.

出现 I/O 错误。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.

CopyTo(ReadOnlySpanAction<Byte,Object>, Object, Int32)

public:
 virtual void CopyTo(System::Buffers::ReadOnlySpanAction<System::Byte, System::Object ^> ^ callback, System::Object ^ state, int bufferSize);
public virtual void CopyTo (System.Buffers.ReadOnlySpanAction<byte,object> callback, object state, int bufferSize);
abstract member CopyTo : System.Buffers.ReadOnlySpanAction<byte, obj> * obj * int -> unit
override this.CopyTo : System.Buffers.ReadOnlySpanAction<byte, obj> * obj * int -> unit
Public Overridable Sub CopyTo (callback As ReadOnlySpanAction(Of Byte, Object), state As Object, bufferSize As Integer)

Parameters

state
Object
bufferSize
Int32

Applies to