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.

目前資料流或 destination 已經在呼叫 CopyTo(Stream) 方法之前關閉。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) 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:
 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 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.

目前資料流或 destination 已經在呼叫 CopyTo(Stream) 方法之前關閉。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.

適用於