Stream.WriteAsync Stream.WriteAsync Stream.WriteAsync Stream.WriteAsync Method

定義

以非同步的方式將位元組序列寫入至目前的資料流,並依寫入的位元組數將資料流中目前的位置往前移。Asynchronously writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.

多載

WriteAsync(Byte[], Int32, Int32, CancellationToken) WriteAsync(Byte[], Int32, Int32, CancellationToken) WriteAsync(Byte[], Int32, Int32, CancellationToken)

以非同步的方式將一連串的位元組寫入目前的資料流,由這個資料流中目前的位置前移寫入的位元組數目,並且監視取消要求。Asynchronously writes a sequence of bytes to the current stream, advances the current position within this stream by the number of bytes written, and monitors cancellation requests.

WriteAsync(ReadOnlyMemory<Byte>, CancellationToken) WriteAsync(ReadOnlyMemory<Byte>, CancellationToken)

以非同步的方式將一連串的位元組寫入目前的資料流,由這個資料流中目前的位置前移寫入的位元組數目,並且監視取消要求。Asynchronously writes a sequence of bytes to the current stream, advances the current position within this stream by the number of bytes written, and monitors cancellation requests.

WriteAsync(Byte[], Int32, Int32) WriteAsync(Byte[], Int32, Int32) WriteAsync(Byte[], Int32, Int32) WriteAsync(Byte[], Int32, Int32)

以非同步的方式將位元組序列寫入至目前的資料流,並依寫入的位元組數將資料流中目前的位置往前移。Asynchronously writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.

WriteAsync(Byte[], Int32, Int32, CancellationToken) WriteAsync(Byte[], Int32, Int32, CancellationToken) WriteAsync(Byte[], Int32, Int32, CancellationToken)

以非同步的方式將一連串的位元組寫入目前的資料流,由這個資料流中目前的位置前移寫入的位元組數目,並且監視取消要求。Asynchronously writes a sequence of bytes to the current stream, advances the current position within this stream by the number of bytes written, and monitors cancellation requests.

public:
 virtual System::Threading::Tasks::Task ^ WriteAsync(cli::array <System::Byte> ^ buffer, int offset, int count, System::Threading::CancellationToken cancellationToken);
[System.Runtime.InteropServices.ComVisible(false)]
public virtual System.Threading.Tasks.Task WriteAsync (byte[] buffer, int offset, int count, System.Threading.CancellationToken cancellationToken);
abstract member WriteAsync : byte[] * int * int * System.Threading.CancellationToken -> System.Threading.Tasks.Task
override this.WriteAsync : byte[] * int * int * System.Threading.CancellationToken -> System.Threading.Tasks.Task

參數

buffer
Byte[]

寫入資料的來源緩衝區。The buffer to write data from.

offset
Int32 Int32 Int32 Int32

buffer 中以零起始的位元組位移,要從其中開始將位元組複製至資料流。The zero-based byte offset in buffer from which to begin copying bytes to the stream.

count
Int32 Int32 Int32 Int32

寫入的最大位元組數。The maximum number of bytes to write.

cancellationToken
CancellationToken CancellationToken CancellationToken CancellationToken

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

傳回

表示非同步寫入作業的工作。A task that represents the asynchronous write operation.

例外狀況

offsetcount 的總和大於緩衝區長度。The sum of offset and count is larger than the buffer length.

資料流不支援寫入。The stream does not support writing.

之前的寫入作業目前正在使用資料流。The stream is currently in use by a previous write operation.

備註

WriteAsync方法可讓您執行需要大量資源的 I/O 作業,而不會封鎖主執行緒。The WriteAsync 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#.

使用CanWrite屬性來判斷目前的執行個體是否支援寫入。Use the CanWrite property to determine whether the current instance supports writing.

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

如需範例,請參閱WriteAsync(Byte[], Int32, Int32)多載。For an example, see the WriteAsync(Byte[], Int32, Int32) overload.

另請參閱

WriteAsync(ReadOnlyMemory<Byte>, CancellationToken) WriteAsync(ReadOnlyMemory<Byte>, CancellationToken)

以非同步的方式將一連串的位元組寫入目前的資料流,由這個資料流中目前的位置前移寫入的位元組數目,並且監視取消要求。Asynchronously writes a sequence of bytes to the current stream, advances the current position within this stream by the number of bytes written, and monitors cancellation requests.

public virtual System.Threading.Tasks.ValueTask WriteAsync (ReadOnlyMemory<byte> buffer, System.Threading.CancellationToken cancellationToken = null);
abstract member WriteAsync : ReadOnlyMemory<byte> * System.Threading.CancellationToken -> System.Threading.Tasks.ValueTask
override this.WriteAsync : ReadOnlyMemory<byte> * System.Threading.CancellationToken -> System.Threading.Tasks.ValueTask

參數

buffer
ReadOnlyMemory<Byte>

資料寫入來源的記憶體區域。The region of memory to write data from.

cancellationToken
CancellationToken CancellationToken CancellationToken CancellationToken

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

傳回

表示非同步寫入作業的工作。A task that represents the asynchronous write operation.

備註

WriteAsync方法可讓您執行需要大量資源的 I/O 作業,而不會封鎖主執行緒。The WriteAsync 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#.

使用CanWrite屬性來判斷目前的執行個體是否支援寫入。Use the CanWrite property to determine whether the current instance supports writing.

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

如需範例,請參閱WriteAsync(Byte[], Int32, Int32)多載。For an example, see the WriteAsync(Byte[], Int32, Int32) overload.

WriteAsync(Byte[], Int32, Int32) WriteAsync(Byte[], Int32, Int32) WriteAsync(Byte[], Int32, Int32) WriteAsync(Byte[], Int32, Int32)

以非同步的方式將位元組序列寫入至目前的資料流,並依寫入的位元組數將資料流中目前的位置往前移。Asynchronously writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.

public:
 System::Threading::Tasks::Task ^ WriteAsync(cli::array <System::Byte> ^ buffer, int offset, int count);
[System.Runtime.InteropServices.ComVisible(false)]
public System.Threading.Tasks.Task WriteAsync (byte[] buffer, int offset, int count);
member this.WriteAsync : byte[] * int * int -> System.Threading.Tasks.Task
Public Function WriteAsync (buffer As Byte(), offset As Integer, count As Integer) As Task

參數

buffer
Byte[]

寫入資料的來源緩衝區。The buffer to write data from.

offset
Int32 Int32 Int32 Int32

buffer 中以零起始的位元組位移,要從其中開始將位元組複製至資料流。The zero-based byte offset in buffer from which to begin copying bytes to the stream.

count
Int32 Int32 Int32 Int32

寫入的最大位元組數。The maximum number of bytes to write.

傳回

表示非同步寫入作業的工作。A task that represents the asynchronous write operation.

例外狀況

offsetcount 的總和大於緩衝區長度。The sum of offset and count is larger than the buffer length.

資料流不支援寫入。The stream does not support writing.

之前的寫入作業目前正在使用資料流。The stream is currently in use by a previous write operation.

範例

下列範例示範如何以非同步方式寫入檔案。The following example shows how to write asynchronously to a file. 此範例會使用FileStream類別,衍生自Stream類別。The example uses the FileStream class, which derives from the Stream class.

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

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

        private async void Button_Click(object sender, RoutedEventArgs e)
        {
            UnicodeEncoding uniencoding = new UnicodeEncoding();
            string filename = @"c:\Users\exampleuser\Documents\userinputlog.txt";
           
            byte[] result = uniencoding.GetBytes(UserInput.Text);
            
            using (FileStream SourceStream = File.Open(filename, FileMode.OpenOrCreate))
            {
                SourceStream.Seek(0, SeekOrigin.End);
                await SourceStream.WriteAsync(result, 0, result.Length);
            }
        }
    }
}
Imports System.IO
Imports System.Text

Class MainWindow
    Private Async Sub Button_Click(sender As Object, e As RoutedEventArgs)
        Dim uniencoding As UnicodeEncoding = New UnicodeEncoding()
        Dim filename As String = "c:\Users\exampleuser\Documents\userinputlog.txt"

        Dim result As Byte() = uniencoding.GetBytes(UserInput.Text)

        Using SourceStream As FileStream = File.Open(filename, FileMode.OpenOrCreate)
            SourceStream.Seek(0, SeekOrigin.End)
            Await SourceStream.WriteAsync(result, 0, result.Length)
        End Using
    End Sub
End Class

備註

WriteAsync方法可讓您執行需要大量資源的 I/O 作業,而不會封鎖主執行緒。The WriteAsync 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#.

使用CanWrite屬性來判斷目前的執行個體是否支援寫入。Use the CanWrite property to determine whether the current instance supports writing.

適用於