StreamWriter.WriteAsync Method
Definition
Asynchronously writes data to the stream.
Overloads
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
Parameters
- value
- Char
The character to write to the stream.
Returns
A task that represents the asynchronous write operation.
- Attributes
Exceptions
The stream writer is disposed.
The stream writer is currently in use by a previous write operation.
Examples
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
Applies to
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
Parameters
- value
- String
The string to write to the stream. If value
is null
, nothing is written.
Returns
A task that represents the asynchronous write operation.
- Attributes
Exceptions
The stream writer is disposed.
The stream writer is currently in use by a previous write operation.
Examples
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
Applies to
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);
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
Parameters
- buffer
- ReadOnlyMemory<Char>
The character memory region to write to the stream.
- cancellationToken
- CancellationToken
The token to monitor for cancellation requests. The default value is None.
Returns
A task that represents the asynchronous write operation.
Applies to
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
Parameters
- 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.
Returns
A task that represents the asynchronous write operation.
- Attributes
Exceptions
buffer
is null
.
The index
plus count
is greater than the buffer length.
index
or count
is negative.
The stream writer is disposed.
The stream writer is currently in use by a previous write operation.
Examples
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