Läs på engelska Redigera

Dela via


StreamWriter.WriteLineAsync Method

Definition

Asynchronously writes data to the stream, followed by a line terminator.

Overloads

WriteLineAsync(ReadOnlyMemory<Char>, CancellationToken)

Asynchronously writes the text representation of a character memory region to the stream, followed by a line terminator.

WriteLineAsync()

Asynchronously writes a line terminator to the stream.

WriteLineAsync(Char)

Asynchronously writes a character to the stream, followed by a line terminator.

WriteLineAsync(String)

Asynchronously writes a string to the stream, followed by a line terminator.

WriteLineAsync(Char[], Int32, Int32)

Asynchronously writes a subarray of characters to the stream, followed by a line terminator.

WriteLineAsync(ReadOnlyMemory<Char>, CancellationToken)

Source:
StreamWriter.cs
Source:
StreamWriter.cs
Source:
StreamWriter.cs

Asynchronously writes the text representation of a character memory region to the stream, followed by a line terminator.

C#
public override System.Threading.Tasks.Task WriteLineAsync(ReadOnlyMemory<char> buffer, System.Threading.CancellationToken cancellationToken = default);

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.

Exceptions

The cancellation token was canceled. This exception is stored into the returned task.

Remarks

The line terminator is defined by the CoreNewLine field.

Applies to

.NET 10 och andra versioner
Produkt Versioner
.NET Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Standard 2.1

WriteLineAsync()

Source:
StreamWriter.cs
Source:
StreamWriter.cs
Source:
StreamWriter.cs

Asynchronously writes a line terminator to the stream.

C#
public override System.Threading.Tasks.Task WriteLineAsync();
C#
[System.Runtime.InteropServices.ComVisible(false)]
public override System.Threading.Tasks.Task WriteLineAsync();

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.

Remarks

The line terminator is defined by the TextWriter.NewLine property.

Applies to

.NET 10 och andra versioner
Produkt Versioner
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

WriteLineAsync(Char)

Source:
StreamWriter.cs
Source:
StreamWriter.cs
Source:
StreamWriter.cs

Asynchronously writes a character to the stream, followed by a line terminator.

C#
public override System.Threading.Tasks.Task WriteLineAsync(char value);
C#
[System.Runtime.InteropServices.ComVisible(false)]
public override System.Threading.Tasks.Task WriteLineAsync(char value);

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 line in a text file, followed by another line that contains a single character (the letter "b"), by using the WriteLineAsync(Char) method.

C#
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');
            }
        }
    }
}

Remarks

The line terminator is defined by the TextWriter.NewLine property.

Applies to

.NET 10 och andra versioner
Produkt Versioner
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

WriteLineAsync(String)

Source:
StreamWriter.cs
Source:
StreamWriter.cs
Source:
StreamWriter.cs

Asynchronously writes a string to the stream, followed by a line terminator.

C#
public override System.Threading.Tasks.Task WriteLineAsync(string value);
C#
public override System.Threading.Tasks.Task WriteLineAsync(string? value);
C#
[System.Runtime.InteropServices.ComVisible(false)]
public override System.Threading.Tasks.Task WriteLineAsync(string value);

Parameters

value
String

The string to write. If the value is null, only a line terminator 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 two lines that consist of string values to a text file by using the WriteLineAsync(String) method.

C#
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");
            }
        }
    }
}

Remarks

The line terminator is defined by the TextWriter.NewLine property.

This method stores in the task it returns all non-usage exceptions that the method's synchronous counterpart can throw. If an exception is stored into the returned task, that exception will be thrown when the task is awaited. Usage exceptions, such as ArgumentException, are still thrown synchronously. For the stored exceptions, see the exceptions thrown by WriteLine(String).

Applies to

.NET 10 och andra versioner
Produkt Versioner
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

WriteLineAsync(Char[], Int32, Int32)

Source:
StreamWriter.cs
Source:
StreamWriter.cs
Source:
StreamWriter.cs

Asynchronously writes a subarray of characters to the stream, followed by a line terminator.

C#
public override System.Threading.Tasks.Task WriteLineAsync(char[] buffer, int index, int count);
C#
[System.Runtime.InteropServices.ComVisible(false)]
public override System.Threading.Tasks.Task WriteLineAsync(char[] buffer, int index, int count);

Parameters

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.

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 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").

C#
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);
            }
        }
    }
}

Remarks

The line terminator is defined by the TextWriter.NewLine property.

Applies to

.NET 10 och andra versioner
Produkt Versioner
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0