StreamReader.ReadToEndAsync Metodo

Definizione

Legge tutti i caratteri dalla posizione corrente alla fine del flusso in modo asincrono e li restituisce come una stringa.

public:
 override System::Threading::Tasks::Task<System::String ^> ^ ReadToEndAsync();
public override System.Threading.Tasks.Task<string> ReadToEndAsync();
[System.Runtime.InteropServices.ComVisible(false)]
public override System.Threading.Tasks.Task<string> ReadToEndAsync();
override this.ReadToEndAsync : unit -> System.Threading.Tasks.Task<string>
[<System.Runtime.InteropServices.ComVisible(false)>]
override this.ReadToEndAsync : unit -> System.Threading.Tasks.Task<string>
Public Overrides Function ReadToEndAsync () As Task(Of String)

Valori restituiti

Attività che rappresenta l'operazione di lettura asincrona. Il valore del TResult parametro contiene una stringa con i caratteri dalla posizione corrente alla fine del flusso.

Attributi

Eccezioni

Il numero di caratteri è maggiore di Int32.MaxValue.

Il flusso è stato eliminato.

Il lettore è attualmente in uso da un'operazione di lettura precedente.

Esempio

Nell'esempio seguente viene illustrato come leggere il contenuto di un file usando il ReadToEndAsync() metodo .

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.ReadToEndAsync();
                Console.WriteLine("Contains: " + result);
            }
        }
    }
}
Imports System.IO

Module Module1

    Sub Main()
        ReadCharacters()
    End Sub

    Async Sub ReadCharacters()
        Dim result As String

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

Commenti

Questo metodo archivia nell'attività che restituisce tutte le eccezioni non di utilizzo che la controparte sincrona del metodo può generare. Se un'eccezione viene archiviata nell'attività restituita, tale eccezione verrà generata quando l'attività è attesa. Le eccezioni di utilizzo, ad esempio ArgumentException, vengono comunque generate in modo sincrono. Per le eccezioni archiviate, vedere le eccezioni generate da ReadToEnd().

Si applica a