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

定义

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

重载

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.

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)

参数

destination
Stream Stream Stream Stream

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

异常

当前流不支持读取。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.

示例

以下示例将复制的内容FileStreamMemoryStreamThe 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) 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)

参数

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

异常

当前流不支持读取。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.

适用于