StreamWriter.WriteLineAsync 方法

定義

以非同步方式將資料寫入資料流,後接行結束字元。

多載

WriteLineAsync(ReadOnlyMemory<Char>, CancellationToken)

以非同步方式將字元記憶體區域的文字表示寫入資料流,後接行結束字元。

WriteLineAsync()

以非同步方式將行結束字元寫入資料流。

WriteLineAsync(Char)

以非同步方式將字元寫入資料流,後接行結束字元。

WriteLineAsync(String)

以非同步方式將字串寫入資料流,後接行結束字元。

WriteLineAsync(Char[], Int32, Int32)

以非同步方式將字元的子陣列寫入資料流,後接行結束字元。

WriteLineAsync(ReadOnlyMemory<Char>, CancellationToken)

以非同步方式將字元記憶體區域的文字表示寫入資料流,後接行結束字元。

public override System.Threading.Tasks.Task WriteLineAsync (ReadOnlyMemory<char> buffer, System.Threading.CancellationToken cancellationToken = default);
override this.WriteLineAsync : ReadOnlyMemory<char> * System.Threading.CancellationToken -> System.Threading.Tasks.Task
Public Overrides Function WriteLineAsync (buffer As ReadOnlyMemory(Of Char), Optional cancellationToken As CancellationToken = Nothing) As Task

參數

buffer
ReadOnlyMemory<Char>

要寫入資料流的字元記憶體區域。

cancellationToken
CancellationToken

用來監視是否有取消要求的語彙基元。 預設值是 None

傳回

Task

表示非同步寫入作業的工作。

備註

行結束字元是由 CoreNewLine 欄位所定義。

適用於

WriteLineAsync()

以非同步方式將行結束字元寫入資料流。

public:
 override System::Threading::Tasks::Task ^ WriteLineAsync();
public override System.Threading.Tasks.Task WriteLineAsync ();
[System.Runtime.InteropServices.ComVisible(false)]
public override System.Threading.Tasks.Task WriteLineAsync ();
override this.WriteLineAsync : unit -> System.Threading.Tasks.Task
[<System.Runtime.InteropServices.ComVisible(false)>]
override this.WriteLineAsync : unit -> System.Threading.Tasks.Task
Public Overrides Function WriteLineAsync () As Task

傳回

Task

表示非同步寫入作業的工作。

屬性

例外狀況

資料流寫入器已處置。

資料流寫入器目前由先前寫入作業所使用。

備註

行結束字元是由 屬性所 TextWriter.NewLine 定義。

適用於

WriteLineAsync(Char)

以非同步方式將字元寫入資料流,後接行結束字元。

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

參數

value
Char

要寫入資料流的字元。

傳回

Task

表示非同步寫入作業的工作。

屬性

例外狀況

資料流寫入器已處置。

資料流寫入器目前由先前寫入作業所使用。

範例

下列範例示範如何使用 方法,將單一字元 (字母 「a」) 寫入文字檔中的一行, WriteLineAsync(Char) 後面接著另一行,其中包含單一字元 (字母 「b」) 。

using System.IO;

namespace ConsoleApplication
{
    class Program3
    {
        static void Main()
        {
            WriteCharacters();
        }

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

Module Module1

    Sub Main()
        WriteCharacters()
    End Sub

    Async Sub WriteCharacters()
        Dim firstChar As Char = "a"
        Dim secondChar As Char = "b"
        Using writer As StreamWriter = File.CreateText("newfile.txt")
            Await writer.WriteLineAsync(firstChar)
            Await writer.WriteLineAsync(secondChar)
        End Using
    End Sub
End Module

備註

行結束字元是由 屬性所 TextWriter.NewLine 定義。

適用於

WriteLineAsync(String)

以非同步方式將字串寫入資料流,後接行結束字元。

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

參數

value
String

要寫入的字串。 如果值為 null,則只會寫入行結束字元。

傳回

Task

表示非同步寫入作業的工作。

屬性

例外狀況

資料流寫入器已處置。

資料流寫入器目前由先前寫入作業所使用。

範例

下列範例示範如何使用 方法,將包含字串值的兩行寫入文字檔 WriteLineAsync(String)

using System.IO;

namespace ConsoleApplication
{
    class Program4
    {
        static void Main()
        {
            WriteCharacters();
        }

        static async void WriteCharacters()
        {
            using (StreamWriter writer = File.CreateText("newfile.txt"))
            {
                await writer.WriteLineAsync("First line of example");
                await writer.WriteLineAsync("and second line");
            }
        }
    }
}
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.WriteLineAsync("First line of example")
            Await writer.WriteLineAsync("and second line")
        End Using
    End Sub
End Module

備註

行結束字元是由 屬性所 TextWriter.NewLine 定義。

適用於

WriteLineAsync(Char[], Int32, Int32)

以非同步方式將字元的子陣列寫入資料流,後接行結束字元。

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

參數

buffer
Char[]

資料寫入來源的字元陣列。

index
Int32

緩衝區中要開始讀取資料的字元位置。

count
Int32

要寫入的最大字元數。

傳回

Task

表示非同步寫入作業的工作。

屬性

例外狀況

buffernull

index 加上 count 大於緩衝區長度。

indexcount 為負。

資料流寫入器已處置。

資料流寫入器目前由先前寫入作業所使用。

範例

下列範例示範如何使用 方法,將字元寫入文字檔 WriteLineAsync(Char[], Int32, Int32) 中的兩行。 第一行包含字串的前 11 個字元, (字母 「First line」 後面接著空格) 。 第二行包含字串中的其餘字元, (字母 「and second line」 ) 。

using System.IO;
using System.Text;

namespace ConsoleApplication
{
    class Program6
    {
        static void Main()
        {
            WriteCharacters();
        }

        static async void WriteCharacters()
        {
            UnicodeEncoding ue = new UnicodeEncoding();
            char[] charsToAdd = ue.GetChars(ue.GetBytes("First line and second line"));
            using (StreamWriter writer = File.CreateText("newfile.txt"))
            {
                await writer.WriteLineAsync(charsToAdd, 0, 11);
                await writer.WriteLineAsync(charsToAdd, 11, charsToAdd.Length - 11);
            }
        }
    }
}
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("First line and second line"))

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

備註

行結束字元是由 屬性所 TextWriter.NewLine 定義。

適用於