StreamWriter.WriteAsync 方法

定义

将数据异步写入该流。Asynchronously writes data to the stream.

重载

WriteAsync(Char)

将字符异步写入该流。Asynchronously writes a character to the stream.

WriteAsync(String)

将字符串异步写入该流。Asynchronously writes a string to the stream.

WriteAsync(ReadOnlyMemory<Char>, CancellationToken)

将字符内存区域异步写入该流。Asynchronously writes a character memory region to the stream.

WriteAsync(Char[], Int32, Int32)

将字符的子数组异步写入该流。Asynchronously writes a subarray of characters to the stream.

WriteAsync(Char)

将字符异步写入该流。Asynchronously writes a character to the stream.

public:
 override System::Threading::Tasks::Task ^ WriteAsync(char value);
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
[<System.Runtime.InteropServices.ComVisible(false)>]
override this.WriteAsync : char -> System.Threading.Tasks.Task
Public Overrides Function WriteAsync (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" ) 写入文本文件中 WriteAsync(Char)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)

将字符串异步写入该流。Asynchronously writes a string to the stream.

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

参数

value
String

要写入流的字符串。The string to write to the stream. 如果 valuenull,则不写入任何内容。If value is null, nothing 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.

示例

下面的示例演示如何使用方法将字符串写入文本文件 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)

将字符内存区域异步写入该流。Asynchronously writes a character memory region to the stream.

public override System.Threading.Tasks.Task? WriteAsync (ReadOnlyMemory<char> buffer, System.Threading.CancellationToken cancellationToken = default);
public override System.Threading.Tasks.Task WriteAsync (ReadOnlyMemory<char> buffer, System.Threading.CancellationToken cancellationToken = default);
override this.WriteAsync : ReadOnlyMemory<char> * System.Threading.CancellationToken -> System.Threading.Tasks.Task
Public Overrides Function WriteAsync (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.

适用于

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);
public override System.Threading.Tasks.Task WriteAsync (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
[<System.Runtime.InteropServices.ComVisible(false)>]
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

在开始读取数据时缓冲区中的字符位置。The character position in the buffer at which to begin 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.

示例

下面的示例演示如何使用方法将多个字符写入文本文件 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

适用于