StreamWriter.WriteLineAsync 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
以异步方式将数据写入流,后跟行终止符。
重载
| 名称 | 说明 |
|---|---|
| WriteLineAsync(ReadOnlyMemory<Char>, CancellationToken) |
将字符内存区域的文本表示形式异步写入流,后跟行终止符。 |
| WriteLineAsync() |
将行终止符异步写入流。 |
| WriteLineAsync(Char) |
将字符异步写入流,后跟行终止符。 |
| WriteLineAsync(String) |
以异步方式将字符串写入流,后跟行终止符。 |
| WriteLineAsync(Char[], Int32, Int32) |
将字符的子数组异步写入流,后跟行终止符。 |
WriteLineAsync(ReadOnlyMemory<Char>, CancellationToken)
- Source:
- StreamWriter.cs
- Source:
- StreamWriter.cs
- Source:
- StreamWriter.cs
- Source:
- StreamWriter.cs
- Source:
- StreamWriter.cs
将字符内存区域的文本表示形式异步写入流,后跟行终止符。
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。
返回
表示异步写入作的任务。
例外
取消令牌已取消。 此异常存储在返回的任务中。
注解
行终止符由 CoreNewLine 字段定义。
适用于
WriteLineAsync()
- Source:
- StreamWriter.cs
- Source:
- StreamWriter.cs
- Source:
- StreamWriter.cs
- Source:
- StreamWriter.cs
- Source:
- StreamWriter.cs
将行终止符异步写入流。
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
返回
表示异步写入作的任务。
- 属性
例外
流编写器已释放。
流编写器当前正由以前的写入操作使用。
注解
行终止符由 TextWriter.NewLine 属性定义。
适用于
WriteLineAsync(Char)
- Source:
- StreamWriter.cs
- Source:
- StreamWriter.cs
- Source:
- StreamWriter.cs
- Source:
- StreamWriter.cs
- Source:
- StreamWriter.cs
将字符异步写入流,后跟行终止符。
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
要写入流的字符。
返回
表示异步写入作的任务。
- 属性
例外
流编写器已释放。
流编写器当前正由以前的写入操作使用。
示例
下面的示例演示如何使用 WriteLineAsync(Char) 该方法将单个字符(字母“a”)写入文本文件中的一行,后跟另一行,其中包含单个字符(字母“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)
- Source:
- StreamWriter.cs
- Source:
- StreamWriter.cs
- Source:
- StreamWriter.cs
- Source:
- StreamWriter.cs
- Source:
- StreamWriter.cs
以异步方式将字符串写入流,后跟行终止符。
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则只写入行终止符。
返回
表示异步写入作的任务。
- 属性
例外
流编写器已释放。
流编写器当前正由以前的写入操作使用。
示例
以下示例演示如何使用 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 属性定义。
此方法存储在任务中,它返回该方法的同步对应项可以引发的所有非使用异常。 如果异常存储在返回的任务中,则等待任务时将引发该异常。 使用情况异常(例如 ArgumentException)仍会同步引发。 有关存储的异常,请参阅由 WriteLine(String)..
适用于
WriteLineAsync(Char[], Int32, Int32)
- Source:
- StreamWriter.cs
- Source:
- StreamWriter.cs
- Source:
- StreamWriter.cs
- Source:
- StreamWriter.cs
- Source:
- StreamWriter.cs
将字符的子数组异步写入流,后跟行终止符。
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
要写入的最大字符数。
返回
表示异步写入作的任务。
- 属性
例外
buffer 是 null。
index加号count大于缓冲区长度。
index 或 count 为负数。
流编写器已释放。
流编写器当前正由以前的写入操作使用。
示例
以下示例演示如何使用 WriteLineAsync(Char[], Int32, Int32) 该方法将字符写入文本文件中的两个单独的行。 第一行包含字符串的前 11 个字符(字母“第一行”后跟空格)。 第二行包含字符串(字母“和第二行”)的剩余字符。
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 属性定义。