StreamWriter.WriteAsync
Method
Definition
Overloads
| WriteAsync(Char) |
Writes a character to the stream asynchronously. |
| WriteAsync(String) |
Writes a string to the stream asynchronously. |
| WriteAsync(Char[], Int32, Int32) |
Writes a subarray of characters to the stream asynchronously. |
WriteAsync(Char)
Writes a character to the stream asynchronously.
[System.Runtime.InteropServices.ComVisible(false)]
public override System.Threading.Tasks.Task WriteAsync (char value);
- value
- Char
The character to write to the stream.
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.
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
WriteAsync(String)
Writes a string to the stream asynchronously.
[System.Runtime.InteropServices.ComVisible(false)]
public override System.Threading.Tasks.Task WriteAsync (string value);
- value
- String
The string to write to the stream. If value is null, nothing is written.
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.
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
WriteAsync(Char[], Int32, Int32)
Writes a subarray of characters to the stream asynchronously.
[System.Runtime.InteropServices.ComVisible(false)]
public override System.Threading.Tasks.Task WriteAsync (char[] buffer, int index, int count);
- 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.
A task that represents the asynchronous write operation.
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