Stream.CopyToAsync Stream.CopyToAsync Stream.CopyToAsync Stream.CopyToAsync Method

定義

以非同步的方式從目前資料流讀取所有位元組,並將其寫入另一個資料流中。Asynchronously reads the bytes from the current stream and writes them to another stream.

多載

CopyToAsync(Stream, Int32, CancellationToken) CopyToAsync(Stream, Int32, CancellationToken) CopyToAsync(Stream, Int32, CancellationToken)

使用指定的緩衝區大小和取消語彙基元,以非同步的方式從目前資料流讀取位元組,並將其寫入另一個資料流。Asynchronously reads the bytes from the current stream and writes them to another stream, using a specified buffer size and cancellation token.

CopyToAsync(Stream, CancellationToken) CopyToAsync(Stream, CancellationToken) CopyToAsync(Stream, CancellationToken)

使用指定的取消權杖,以非同步的方式從目前資料流讀取位元組,並將其寫入另一個資料流。Asynchronously reads the bytes from the current stream and writes them to another stream, using a specified cancellation token.

CopyToAsync(Stream) CopyToAsync(Stream) CopyToAsync(Stream) CopyToAsync(Stream)

以非同步的方式從目前資料流讀取所有位元組,並將其寫入另一個資料流中。Asynchronously reads the bytes from the current stream and writes them to another stream.

CopyToAsync(Stream, Int32) CopyToAsync(Stream, Int32) CopyToAsync(Stream, Int32) CopyToAsync(Stream, Int32)

使用指定的緩衝區大小,以非同步的方式從目前資料流讀取所有位元組,並將其寫入另一個資料流中。Asynchronously reads the bytes from the current stream and writes them to another stream, using a specified buffer size.

CopyToAsync(Stream, Int32, CancellationToken) CopyToAsync(Stream, Int32, CancellationToken) CopyToAsync(Stream, Int32, CancellationToken)

使用指定的緩衝區大小和取消語彙基元,以非同步的方式從目前資料流讀取位元組,並將其寫入另一個資料流。Asynchronously reads the bytes from the current stream and writes them to another stream, using a specified buffer size and cancellation token.

public:
 virtual System::Threading::Tasks::Task ^ CopyToAsync(System::IO::Stream ^ destination, int bufferSize, System::Threading::CancellationToken cancellationToken);
[System.Runtime.InteropServices.ComVisible(false)]
public virtual System.Threading.Tasks.Task CopyToAsync (System.IO.Stream destination, int bufferSize, System.Threading.CancellationToken cancellationToken);
abstract member CopyToAsync : System.IO.Stream * int * System.Threading.CancellationToken -> System.Threading.Tasks.Task
override this.CopyToAsync : System.IO.Stream * int * System.Threading.CancellationToken -> System.Threading.Tasks.Task

參數

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, in bytes, of the buffer. 這個值必須大於零。This value must be greater than zero. 預設大小為 81920。The default size is 81920.

cancellationToken
CancellationToken CancellationToken CancellationToken CancellationToken

用來監視是否有取消要求的語彙基元。The token to monitor for cancellation requests. 預設值為 NoneThe default value is None.

傳回

表示非同步複製作業的工作。A task that represents the asynchronous copy operation.

例外狀況

目前資料流或目的資料流已處置。Either the current stream or the destination stream is disposed.

目前資料流不支援讀取,或目的資料流不支援寫入。The current stream does not support reading, or the destination stream does not support writing.

備註

CopyToAsync方法可讓您執行需要大量資源的 I/O 作業,而不會封鎖主執行緒。The CopyToAsync method enables you to perform resource-intensive I/O operations without blocking the main thread. 這項效能考量對於 Windows 8.x 市集Windows 8.x Store 應用程式或 桌面desktop 應用程式而言特別重要,尤其是針對耗時的資料流作業可能會阻礙 UI 執行緒,使應用程式看起來像是停止運作的情況。This performance consideration is particularly important in a Windows 8.x 市集Windows 8.x Store app or 桌面desktop app where a time-consuming stream operation can block the UI thread and make your app appear as if it is not working. 非同步方法會用於搭配asyncawaitVisual Basic 和 C# 中的關鍵字。The async methods are used in conjunction with the async and await keywords in Visual Basic and C#.

如果在作業取消之前完成,傳回的工作就會包含CanceledStatus屬性。If the operation is canceled before it completes, the returned task contains the Canceled value for the Status property.

在目前資料流目前位置開始複製。Copying begins at the current position in the current stream.

如需兩個資料流之間進行複製的範例,請參閱CopyToAsync(Stream)多載。For an example of copying between two streams, see the CopyToAsync(Stream) overload.

CopyToAsync(Stream, CancellationToken) CopyToAsync(Stream, CancellationToken) CopyToAsync(Stream, CancellationToken)

使用指定的取消權杖,以非同步的方式從目前資料流讀取位元組,並將其寫入另一個資料流。Asynchronously reads the bytes from the current stream and writes them to another stream, using a specified cancellation token.

public:
 System::Threading::Tasks::Task ^ CopyToAsync(System::IO::Stream ^ destination, System::Threading::CancellationToken cancellationToken);
public System.Threading.Tasks.Task CopyToAsync (System.IO.Stream destination, System.Threading.CancellationToken cancellationToken);
member this.CopyToAsync : System.IO.Stream * System.Threading.CancellationToken -> System.Threading.Tasks.Task

參數

destination
Stream Stream Stream Stream

目前資料流的內容將複製到其中的資料流。The stream to which the contents of the current stream will be copied.

cancellationToken
CancellationToken CancellationToken CancellationToken CancellationToken

用來監視是否有取消要求的語彙基元。The token to monitor for cancellation requests. 預設值為 NoneThe default value is None.

傳回

表示非同步複製作業的工作。A task that represents the asynchronous copy operation.

備註

CopyToAsync方法可讓您執行需要大量資源的 I/O 作業,而不會封鎖主執行緒。The CopyToAsync method enables you to perform resource-intensive I/O operations without blocking the main thread. 這項效能考量對於 Windows 8.x 市集Windows 8.x Store 應用程式或 桌面desktop 應用程式而言特別重要,尤其是針對耗時的資料流作業可能會阻礙 UI 執行緒,使應用程式看起來像是停止運作的情況。This performance consideration is particularly important in a Windows 8.x 市集Windows 8.x Store app or 桌面desktop app where a time-consuming stream operation can block the UI thread and make your app appear as if it is not working. 非同步方法會用於搭配asyncawaitVisual Basic 和 C# 中的關鍵字。The async methods are used in conjunction with the async and await keywords in Visual Basic and C#.

如果在作業取消之前完成,傳回的工作就會包含CanceledStatus屬性。If the operation is canceled before it completes, the returned task contains the Canceled value for the Status property.

在目前資料流目前位置開始複製。Copying begins at the current position in the current stream.

如需兩個資料流之間進行複製的範例,請參閱CopyToAsync(Stream)多載。For an example of copying between two streams, see the CopyToAsync(Stream) overload.

CopyToAsync(Stream) CopyToAsync(Stream) CopyToAsync(Stream) CopyToAsync(Stream)

以非同步的方式從目前資料流讀取所有位元組,並將其寫入另一個資料流中。Asynchronously reads the bytes from the current stream and writes them to another stream.

public:
 System::Threading::Tasks::Task ^ CopyToAsync(System::IO::Stream ^ destination);
[System.Runtime.InteropServices.ComVisible(false)]
public System.Threading.Tasks.Task CopyToAsync (System.IO.Stream destination);
member this.CopyToAsync : System.IO.Stream -> System.Threading.Tasks.Task
Public Function CopyToAsync (destination As Stream) As Task

參數

destination
Stream Stream Stream Stream

目前資料流的內容將複製到其中的資料流。The stream to which the contents of the current stream will be copied.

傳回

表示非同步複製作業的工作。A task that represents the asynchronous copy operation.

例外狀況

目前資料流或目的資料流已處置。Either the current stream or the destination stream is disposed.

目前資料流不支援讀取,或目的資料流不支援寫入。The current stream does not support reading, or the destination stream does not support writing.

範例

下列範例示範如何使用兩個FileStream要以非同步方式將檔案從一個目錄複製到另一個物件。The following example demonstrates how to use two FileStream objects to asynchronously copy the files from one directory to another. FileStream 類別衍生自 Stream 類別。The FileStream class derives from the Stream class. 請注意,Click事件處理常式Button控制項,會以標記async修飾詞因為它會呼叫非同步方法Notice that the Click event handler for the Button control is marked with the async modifier because it calls an asynchronous method

using System;
using System.Threading.Tasks;
using System.Windows;
using System.IO;

namespace WpfApplication
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private async void Button_Click(object sender, RoutedEventArgs e)
        {
            string StartDirectory = @"c:\Users\exampleuser\start";
            string EndDirectory = @"c:\Users\exampleuser\end";

            foreach (string filename in Directory.EnumerateFiles(StartDirectory))
            {
                using (FileStream SourceStream = File.Open(filename, FileMode.Open))
                {
                    using (FileStream DestinationStream = File.Create(EndDirectory + filename.Substring(filename.LastIndexOf('\\'))))
                    {
                        await SourceStream.CopyToAsync(DestinationStream);
                    }
                }
            }
        }
    }
}
Imports System.IO

Class MainWindow

    Private Async Sub Button_Click(sender As Object, e As RoutedEventArgs)
        Dim StartDirectory As String = "c:\Users\exampleuser\start"
        Dim EndDirectory As String = "c:\Users\exampleuser\end"

        For Each filename As String In Directory.EnumerateFiles(StartDirectory)
            Using SourceStream As FileStream = File.Open(filename, FileMode.Open)
                Using DestinationStream As FileStream = File.Create(EndDirectory + filename.Substring(filename.LastIndexOf("\"c)))
                    Await SourceStream.CopyToAsync(DestinationStream)
                End Using

            End Using
        Next
    End Sub

End Class

備註

CopyToAsync方法可讓您執行需要大量資源的 I/O 作業,而不會封鎖主執行緒。The CopyToAsync method enables you to perform resource-intensive I/O operations without blocking the main thread. 這項效能考量對於 Windows 8.x 市集Windows 8.x Store 應用程式或 桌面desktop 應用程式而言特別重要,尤其是針對耗時的資料流作業可能會阻礙 UI 執行緒,使應用程式看起來像是停止運作的情況。This performance consideration is particularly important in a Windows 8.x 市集Windows 8.x Store app or 桌面desktop app where a time-consuming stream operation can block the UI thread and make your app appear as if it is not working. 非同步方法會用於搭配asyncawaitVisual Basic 和 C# 中的關鍵字。The async methods are used in conjunction with the async and await keywords in Visual Basic and C#.

在目前資料流目前位置開始複製。Copying begins at the current position in the current stream.

CopyToAsync(Stream, Int32) CopyToAsync(Stream, Int32) CopyToAsync(Stream, Int32) CopyToAsync(Stream, Int32)

使用指定的緩衝區大小,以非同步的方式從目前資料流讀取所有位元組,並將其寫入另一個資料流中。Asynchronously reads the bytes from the current stream and writes them to another stream, using a specified buffer size.

public:
 System::Threading::Tasks::Task ^ CopyToAsync(System::IO::Stream ^ destination, int bufferSize);
[System.Runtime.InteropServices.ComVisible(false)]
public System.Threading.Tasks.Task CopyToAsync (System.IO.Stream destination, int bufferSize);
member this.CopyToAsync : System.IO.Stream * int -> System.Threading.Tasks.Task
Public Function CopyToAsync (destination As Stream, bufferSize As Integer) As Task

參數

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, in bytes, of the buffer. 這個值必須大於零。This value must be greater than zero. 預設大小為 81920。The default size is 81920.

傳回

表示非同步複製作業的工作。A task that represents the asynchronous copy operation.

例外狀況

目前資料流或目的資料流已處置。Either the current stream or the destination stream is disposed.

目前資料流不支援讀取,或目的資料流不支援寫入。The current stream does not support reading, or the destination stream does not support writing.

備註

CopyToAsync方法可讓您執行需要大量資源的 I/O 作業,而不會封鎖主執行緒。The CopyToAsync method enables you to perform resource-intensive I/O operations without blocking the main thread. 這項效能考量對於 Windows 8.x 市集Windows 8.x Store 應用程式或 桌面desktop 應用程式而言特別重要,尤其是針對耗時的資料流作業可能會阻礙 UI 執行緒,使應用程式看起來像是停止運作的情況。This performance consideration is particularly important in a Windows 8.x 市集Windows 8.x Store app or 桌面desktop app where a time-consuming stream operation can block the UI thread and make your app appear as if it is not working. 非同步方法會用於搭配asyncawaitVisual Basic 和 C# 中的關鍵字。The async methods are used in conjunction with the async and await keywords in Visual Basic and C#.

在目前資料流目前位置開始複製。Copying begins at the current position in the current stream.

如需兩個資料流之間進行複製的範例,請參閱CopyToAsync(Stream)多載。For an example of copying between two streams, see the CopyToAsync(Stream) overload.

適用於