StreamWriter.WriteLineAsync
Method
Definition
Overloads
| WriteLineAsync() |
Writes a line terminator asynchronously to the stream. |
| WriteLineAsync(Char) |
Writes a character followed by a line terminator asynchronously to the stream. |
| WriteLineAsync(String) |
Writes a string followed by a line terminator asynchronously to the stream. |
| WriteLineAsync(Char[], Int32, Int32) |
Writes a subarray of characters followed by a line terminator asynchronously to the stream. |
WriteLineAsync()
Writes a line terminator asynchronously to the stream.
[System.Runtime.InteropServices.ComVisible(false)]
public override System.Threading.Tasks.Task WriteLineAsync ();
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.
Remarks
The line terminator is defined by the TextWriter.NewLine property.
WriteLineAsync(Char)
Writes a character followed by a line terminator asynchronously to the stream.
[System.Runtime.InteropServices.ComVisible(false)]
public override System.Threading.Tasks.Task WriteLineAsync (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 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
Remarks
The line terminator is defined by the TextWriter.NewLine property.
WriteLineAsync(String)
Writes a string followed by a line terminator asynchronously to the stream.
[System.Runtime.InteropServices.ComVisible(false)]
public override System.Threading.Tasks.Task WriteLineAsync (string value);
- value
- String
The string to write. If the value is null, only a line terminator 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 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
Remarks
The line terminator is defined by the TextWriter.NewLine property.
WriteLineAsync(Char[], Int32, Int32)
Writes a subarray of characters followed by a line terminator asynchronously to the stream.
[System.Runtime.InteropServices.ComVisible(false)]
public override System.Threading.Tasks.Task WriteLineAsync (char[] buffer, int index, int count);
- 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.
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 characters to two separate lines in a text file by using the WriteLineAsync(Char[], Int32, Int32) method. 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
Remarks
The line terminator is defined by the TextWriter.NewLine property.