StreamWriter.WriteAsync StreamWriter.WriteAsync StreamWriter.WriteAsync StreamWriter.WriteAsync Method

定義

以非同步方式將資料寫入資料流。Asynchronously writes data to the stream.

多載

WriteAsync(Char) WriteAsync(Char) WriteAsync(Char) WriteAsync(Char)

以非同步方式將字元寫入至資料流。Asynchronously writes a character to the stream.

WriteAsync(String) WriteAsync(String) WriteAsync(String) WriteAsync(String)

以非同步方式將字串寫入資料流。Asynchronously writes a string to the stream.

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

以非同步方式將字元記憶體區域寫入資料流。Asynchronously writes a character memory region to the stream.

WriteAsync(Char[], Int32, Int32) WriteAsync(Char[], Int32, Int32) WriteAsync(Char[], Int32, Int32) WriteAsync(Char[], Int32, Int32)

以非同步方式將字元的子陣列寫入資料流。Asynchronously writes a subarray of characters to the stream.

WriteAsync(Char) WriteAsync(Char) WriteAsync(Char) WriteAsync(Char)

以非同步方式將字元寫入至資料流。Asynchronously writes a character to the stream.

public:
 override System::Threading::Tasks::Task ^ WriteAsync(char value);
[System.Runtime.InteropServices.ComVisible(false)]
public override System.Threading.Tasks.Task WriteAsync (char value);
override this.WriteAsync : char -> System.Threading.Tasks.Task
Public Overrides Function WriteAsync (value As Char) As Task

參數

value
Char Char Char Char

要寫入至資料流的字元。The character to write to the stream.

傳回

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

例外狀況

資料流寫入器已處置。The stream writer is disposed.

資料流寫入器目前由先前寫入作業所使用。The stream writer is currently in use by a previous write operation.

範例

下列範例顯示如何使用WriteAsync(Char)方法,將單一字元(字母 "a")寫入文字檔。The following example shows how to write a single character (the letter "a") to a text file by using the WriteAsync(Char) method.

using System;
using System.IO;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            WriteCharacters();
        }

        static async void WriteCharacters()
        {
            using (StreamWriter writer = File.CreateText("newfile.txt"))
            {
                await writer.WriteAsync('a');
            }
        }
    }
}
Imports System.IO
Imports System.Text

Module Module1

    Sub Main()
        WriteCharacters()
    End Sub

    Async Sub WriteCharacters()
        Dim oneLetter As Char = "a"
        Using writer As StreamWriter = File.CreateText("newfile.txt")
            Await writer.WriteAsync(oneLetter)
        End Using
    End Sub
End Module

WriteAsync(String) WriteAsync(String) WriteAsync(String) WriteAsync(String)

以非同步方式將字串寫入資料流。Asynchronously writes a string to the stream.

public:
 override System::Threading::Tasks::Task ^ WriteAsync(System::String ^ value);
[System.Runtime.InteropServices.ComVisible(false)]
public override System.Threading.Tasks.Task WriteAsync (string value);
override this.WriteAsync : string -> System.Threading.Tasks.Task
Public Overrides Function WriteAsync (value As String) As Task

參數

value
String String String String

要寫入資料流的字串。The string to write to the stream. 如果 valuenull,則不寫入任何資料。If value is null, nothing is written.

傳回

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

例外狀況

資料流寫入器已處置。The stream writer is disposed.

資料流寫入器目前由先前寫入作業所使用。The stream writer is currently in use by a previous write operation.

範例

下列範例顯示如何使用WriteAsync(String)方法,將字串寫入文字檔。The following example shows how to write a string to a text file by using the WriteAsync(String) method.

using System;
using System.IO;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            WriteCharacters();
        }

        static async void WriteCharacters()
        {
            using (StreamWriter writer = File.CreateText("newfile.txt"))
            {
                await writer.WriteAsync("Example text as string");
            }
        }
    }
}
Imports System.IO
Imports System.Text

Module Module1

    Sub Main()
        WriteCharacters()
    End Sub

    Async Sub WriteCharacters()

        Using writer As StreamWriter = File.CreateText("newfile.txt")
            Await writer.WriteAsync("Example text as string")
        End Using
    End Sub
End Module

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

以非同步方式將字元記憶體區域寫入資料流。Asynchronously writes a character memory region to the stream.

public override System.Threading.Tasks.Task WriteAsync (ReadOnlyMemory<char> buffer, System.Threading.CancellationToken cancellationToken = null);
override this.WriteAsync : ReadOnlyMemory<char> * System.Threading.CancellationToken -> System.Threading.Tasks.Task

參數

buffer
ReadOnlyMemory<Char>

要寫入資料流的字元記憶體區域。The character memory region to write to the stream.

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(Char[], Int32, Int32) WriteAsync(Char[], Int32, Int32) WriteAsync(Char[], Int32, Int32) WriteAsync(Char[], Int32, Int32)

以非同步方式將字元的子陣列寫入資料流。Asynchronously writes a subarray of characters to the stream.

public:
 override System::Threading::Tasks::Task ^ WriteAsync(cli::array <char> ^ buffer, int index, int count);
[System.Runtime.InteropServices.ComVisible(false)]
public override System.Threading.Tasks.Task WriteAsync (char[] buffer, int index, int count);
override this.WriteAsync : char[] * int * int -> System.Threading.Tasks.Task
Public Overrides Function WriteAsync (buffer As Char(), index As Integer, count As Integer) As Task

參數

buffer
Char[]

含有要寫入之資料的字元陣列。A character array that contains the data to write.

index
Int32 Int32 Int32 Int32

緩衝區中要開始讀取資料的字元位置。The character position in the buffer at which to begin reading data.

count
Int32 Int32 Int32 Int32

要寫入的最大字元數。The maximum number of characters to write.

傳回

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

例外狀況

index 加上 count 大於緩衝區長度。The index plus count is greater than the buffer length.

資料流寫入器已處置。The stream writer is disposed.

資料流寫入器目前由先前寫入作業所使用。The stream writer is currently in use by a previous write operation.

範例

下列範例顯示如何使用WriteAsync(Char[], Int32, Int32)方法,將多個字元寫入文字檔。The following example shows how to write multiple characters to a text file by using the WriteAsync(Char[], Int32, Int32) method.

using System;
using System.Text;
using System.IO;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            WriteCharacters();
        }

        static async void WriteCharacters()
        {
            UnicodeEncoding ue = new UnicodeEncoding();
            char[] charsToAdd = ue.GetChars(ue.GetBytes("Example string"));
            using (StreamWriter writer = File.CreateText("newfile.txt"))
            {
                await writer.WriteAsync(charsToAdd, 0, charsToAdd.Length);
            }
        }
    }
}
Imports System.IO
Imports System.Text

Module Module1

    Sub Main()
        WriteCharacters()
    End Sub

    Async Sub WriteCharacters()
        Dim ue As UnicodeEncoding = New UnicodeEncoding()
        Dim charsToAdd() = ue.GetChars(ue.GetBytes("Example string"))

        Using writer As StreamWriter = File.CreateText("newfile.txt")
            Await writer.WriteAsync(charsToAdd, 0, charsToAdd.Length)
        End Using
    End Sub
End Module

適用於