StreamReader.ReadLineAsync 方法


自目前資料流非同步讀取一行字元,並將資料以字串傳回。Reads a line of characters asynchronously from the current stream and returns the data as a string.

 override System::Threading::Tasks::Task<System::String ^> ^ ReadLineAsync();
public override System.Threading.Tasks.Task<string> ReadLineAsync ();
override this.ReadLineAsync : unit -> System.Threading.Tasks.Task<string>
Public Overrides Function ReadLineAsync () As Task(Of String)


表示非同步讀取作業的工作。A task that represents the asynchronous read operation. TResult 參數的值會包含資料流中的下一行,或者是 null (如果已經讀取所有字元)。The value of the TResult parameter contains the next line from the stream, or is null if all the characters have been read.



下一行的字元數大於 MaxValueThe number of characters in the next line is larger than MaxValue.

資料流已經處置。The stream has been disposed.

之前的讀取作業目前正在使用讀取器。The reader is currently in use by a previous read operation.


下列範例顯示如何使用 ReadLineAsync() 方法來讀取檔案的第一行。The following example shows how to read the first line of a file by using the ReadLineAsync() method.

using System;
using System.IO;

namespace ConsoleApplication
    class Program
        static async Task Main()
            await ReadCharacters();

        static async Task ReadCharacters()
            String result;
            using (StreamReader reader = File.OpenText("existingfile.txt"))
                Console.WriteLine("Opened file.");
                result = await reader.ReadLineAsync();
                Console.WriteLine("First line contains: " + result);
Imports System.IO

Module Module1

    Sub Main()
    End Sub

    Async Sub ReadCharacters()
        Dim result As String

        Using reader As StreamReader = File.OpenText("existingfile.txt")
            Console.WriteLine("Opened file.")
            result = Await reader.ReadLineAsync()
            Console.WriteLine("First line contains: " + result)
        End Using
    End Sub
End Module