Stream.CopyTo 方法

定义

从当前流中读取字节并将其写入到另一流中。 这两个流位置都按复制的字节数提前。

重载

CopyTo(Stream)

从当前流中读取字节并将其写入到另一流中。 这两个流位置都按复制的字节数提前。

CopyTo(Stream, Int32)

使用指定的缓冲区大小,从当前流中读取字节并将其写入到另一流中。 这两个流位置都按复制的字节数提前。

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

当前流的内容将复制到的流。

例外

destinationnull

当前流不支持读取。

- 或 -

destination 不支持写入。

在调用 CopyTo(Stream) 方法前当前流或 destination 已关闭。

出现 I/O 错误。

示例

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

注解

复制从当前流中的当前位置开始,并且不会在复制操作完成后重置目标流的位置。

适用于

CopyTo(Stream, Int32)

使用指定的缓冲区大小,从当前流中读取字节并将其写入到另一流中。 这两个流位置都按复制的字节数提前。

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)

参数

destination
Stream

当前流的内容将复制到的流。

bufferSize
Int32

缓冲区的大小。 此值必须大于零。 默认大小为 81920。

例外

destinationnull

bufferSize 为负数或零。

当前流不支持读取。

- 或 -

destination 不支持写入。

在调用 CopyTo(Stream) 方法前当前流或 destination 已关闭。

出现 I/O 错误。

注解

复制从当前流中的当前位置开始,并且不会在复制操作完成后重置目标流的位置。

适用于