Stream.CopyToAsync 方法

定义

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

重载

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)

通过指定的取消令牌,从当前流中异步读取字节并将其写入到另一个流中。Asynchronously reads the bytes from the current stream and writes them to another stream, using a specified cancellation token.

CopyToAsync(Stream)

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

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)

使用指定的缓冲区大小和取消令牌,从当前流中异步读取字节并将其写入到另一个流中。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);
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

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

bufferSize
Int32

缓冲区的大小(以字节为单位)。The size, in bytes, of the buffer. 此值必须大于零。This value must be greater than zero. 默认大小为 81920。The default size is 81920.

cancellationToken
CancellationToken

要监视取消请求的标记。The token to monitor for cancellation requests. 默认值是 NoneThe default value is None.

返回

表示异步复制操作的任务。A task that represents the asynchronous copy operation.

属性

异常

destinationnulldestination is null.

buffersize 为负数或零。buffersize is negative or zero.

当前流或目标流已释放。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. 异步方法与 Visual Basic 和C#中的 asyncawait 关键字结合使用。The async methods are used in conjunction with the async and await keywords in Visual Basic and C#.

如果在操作完成前取消该操作,则返回的任务将包含 Status 属性的 Canceled 值。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)

通过指定的取消令牌,从当前流中异步读取字节并将其写入到另一个流中。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

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

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. 异步方法与 Visual Basic 和C#中的 asyncawait 关键字结合使用。The async methods are used in conjunction with the async and await keywords in Visual Basic and C#.

如果在操作完成前取消该操作,则返回的任务将包含 Status 属性的 Canceled 值。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)

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

public:
 System::Threading::Tasks::Task ^ CopyToAsync(System::IO::Stream ^ destination);
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

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

返回

表示异步复制操作的任务。A task that represents the asynchronous copy operation.

属性

异常

destinationnulldestination is null.

当前流或目标流已释放。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. 请注意,Button 控件的 Click 事件处理程序使用 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. 异步方法与 Visual Basic 和C#中的 asyncawait 关键字结合使用。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)

使用指定的缓冲区大小,从当前流中异步读取字节并将其写入到另一流中。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);
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

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

bufferSize
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.

属性

异常

destinationnulldestination is null.

buffersize 为负数或零。buffersize is negative or zero.

当前流或目标流已释放。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. 异步方法与 Visual Basic 和C#中的 asyncawait 关键字结合使用。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.

适用于