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)
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)

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

返回

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.

适用于