StreamWriter.WriteLineAsync 方法

定义

将数据异步写入流,后跟行结束符。Asynchronously writes data to the stream, followed by a line terminator.

重载

WriteLineAsync(ReadOnlyMemory<Char>, CancellationToken)

将字符内存区域的文本表示形式异步写入流,后跟行结束符。Asynchronously writes the text representation of a character memory region to the stream, followed by a line terminator.

WriteLineAsync()

将行结束符异步写入流。Asynchronously writes a line terminator to the stream.

WriteLineAsync(Char)

将字符异步写入流,后跟行结束符。Asynchronously writes a character to the stream, followed by a line terminator.

WriteLineAsync(String)

将字符串异步写入流,后跟行结束符。Asynchronously writes a string to the stream, followed by a line terminator.

WriteLineAsync(Char[], Int32, Int32)

将字符的子数组异步写入流,后跟行结束符。Asynchronously writes a subarray of characters to the stream, followed by a line terminator.

WriteLineAsync(ReadOnlyMemory<Char>, CancellationToken)

将字符内存区域的文本表示形式异步写入流,后跟行结束符。Asynchronously writes the text representation of a character memory region to the stream, followed by a line terminator.

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>

要写入流的字符存储区域。The character memory region to write to the stream.

cancellationToken
CancellationToken

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

返回

Task

表示异步写入操作的任务。A task that represents the asynchronous write operation.

注解

行终止符由 CoreNewLine 字段定义。The line terminator is defined by the CoreNewLine field.

适用于

WriteLineAsync()

将行结束符异步写入流。Asynchronously writes a line terminator to the stream.

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

表示异步写入操作的任务。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.

注解

行终止符由 TextWriter.NewLine 属性定义。The line terminator is defined by the TextWriter.NewLine property.

适用于

WriteLineAsync(Char)

将字符异步写入流,后跟行结束符。Asynchronously writes a character to the stream, followed by a line terminator.

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

要写入流中的字符。The character to write to the stream.

返回

Task

表示异步写入操作的任务。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.

示例

下面的示例演示如何使用方法,将字母 "a" ) 中的单个 (字符写入到文本文件中的某一行,后跟另一个包含字母 "b" ) 中的单个字符 (行 WriteLineAsync(Char)The following example shows how to write a single character (the letter "a") to a line in a text file, followed by another line that contains a single character (the letter "b"), by using the WriteLineAsync(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.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 属性定义。The line terminator is defined by the TextWriter.NewLine property.

适用于

WriteLineAsync(String)

将字符串异步写入流,后跟行结束符。Asynchronously writes a string to the stream, followed by a line terminator.

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

要写入的字符串。The string to write. 如果值为 null,则只写入行终止符。If the value is null, only a line terminator is written.

返回

Task

表示异步写入操作的任务。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.

示例

下面的示例演示如何使用方法将包含字符串值的两行写入文本文件 WriteLineAsync(String)The following example shows how to write two lines that consist of string values to a text file by using the WriteLineAsync(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.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 属性定义。The line terminator is defined by the TextWriter.NewLine property.

适用于

WriteLineAsync(Char[], Int32, Int32)

将字符的子数组异步写入流,后跟行结束符。Asynchronously writes a subarray of characters to the stream, followed by a line terminator.

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[]

要从中写出数据的字符数组。The character array to write data from.

index
Int32

在开始读取数据时缓冲区中的字符位置。The character position in the buffer at which to start reading data.

count
Int32

要写入的最大字符数。The maximum number of characters to write.

返回

Task

表示异步写入操作的任务。A task that represents the asynchronous write operation.

属性

例外

buffernullbuffer is null.

indexcount 大于缓冲区长度。The index plus count is greater than the buffer length.

indexcount 为负数。index or count is negative.

流编写器已被释放。The stream writer is disposed.

流编写器正在由其前一次写入操作使用。The stream writer is currently in use by a previous write operation.

示例

下面的示例演示如何使用方法将字符写入文本文件中的两个单独的行 WriteLineAsync(Char[], Int32, Int32)The following example shows how to write characters to two separate lines in a text file by using the WriteLineAsync(Char[], Int32, Int32) method. 第一行包含字符串中的前11个字符, (字母 "First line" 后跟一个空格) 。The first line contains the first 11 characters from the string (the letters "First line" followed by a space). 第二行包含字符串中的剩余字符 (字母 "和第二行" ) 。The second line contains the remaining characters from the string (the letters "and second line").

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("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 属性定义。The line terminator is defined by the TextWriter.NewLine property.

适用于