Stream.CopyTo 方法

定义

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

重载

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

参数

destination
Stream

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

异常

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.

示例

下面的示例将的内容FileStream复制MemoryStream到中。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())

注解

从当前流中的当前位置开始复制,在复制操作完成后,不会重置目标流的位置。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:
 virtual void CopyTo(System::IO::Stream ^ destination, int bufferSize);
public virtual void CopyTo (System.IO.Stream destination, int bufferSize);
abstract member CopyTo : System.IO.Stream * int -> unit
override this.CopyTo : System.IO.Stream * int -> unit
Public Overridable Sub CopyTo (destination As Stream, bufferSize As Integer)

参数

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.

异常

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.

注解

从当前流中的当前位置开始复制,在复制操作完成后,不会重置目标流的位置。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.

适用于