Condividi tramite


Leggere testo da un file

Negli esempi seguenti viene illustrato come leggere il testo in modo sincrono e in modo asincrono da un file di testo usando .NET per le applicazioni desktop. In entrambi gli esempi, quando si crea l'istanza della classe StreamReader, si immette il percorso relativo o assoluto del file.

Nota

Questi esempi di codice non sono applicabili allo sviluppo di applicazioni della piattaforma UWP (Universal Windows Platform), perché il runtime di Windows fornisce tipi di flusso diversi per la lettura e la scrittura di file. Per altre informazioni, vedere Uso di UWP con file. Per esempi che illustrino come eseguire la conversione tra flussi .NET Framework e flussi Windows Runtime, vedere Procedura: Eseguire la conversione tra flussi .NET Framework e flussi Windows Runtime.

Prerequisiti

  • Creare un file di testo denominato TestFile.txt nella stessa cartella dell'app.

    Aggiungere contenuto al file di testo. Gli esempi in questo articolo scrivono il contenuto del file di testo nella console.

Leggere un file

L'esempio seguente mostra un'operazione di lettura sincrona in un'app console. Il contenuto del file viene letto e archiviato in una variabile stringa, che viene quindi scritta nella console.

  1. Creare un'istanza di StreamReader.
  2. Chiamare il metodo StreamReader.ReadToEnd() e assegnare il risultato a una stringa.
  3. Scrivere l'output nella console.
try
{
    // Open the text file using a stream reader.
    using StreamReader reader = new("TestFile.txt");

    // Read the stream as a string.
    string text = reader.ReadToEnd();

    // Write the text to the console.
    Console.WriteLine(text);
}
catch (IOException e)
{
    Console.WriteLine("The file could not be read:");
    Console.WriteLine(e.Message);
}
Try
    ' Open the text file using a stream reader.
    Using reader As New StreamReader("TestFile.txt")

        ' Read the stream as a string.
        Dim text As String = reader.ReadToEnd()

        ' Write the text to the console.
        Console.WriteLine(text)

    End Using
Catch ex As IOException
    Console.WriteLine("The file could not be read:")
    Console.WriteLine(ex.Message)
End Try

Leggere un file in modo asincrono

L'esempio seguente mostra un'operazione di lettura sincrona in un'app console. Il contenuto del file viene letto e archiviato in una variabile stringa, che viene quindi scritta nella console.

  1. Creare un'istanza di StreamReader.
  2. Attendere il metodo StreamReader.ReadToEndAsync() e assegnare il risultato a una stringa.
  3. Scrivere l'output nella console.
try
{
    // Open the text file using a stream reader.
    using StreamReader reader = new("TestFile.txt");

    // Read the stream as a string.
    string text = await reader.ReadToEndAsync();

    // Write the text to the console.
    Console.WriteLine(text);
}
catch (IOException e)
{
    Console.WriteLine("The file could not be read:");
    Console.WriteLine(e.Message);
}
Try
    ' Open the text file using a stream reader.
    Using reader As New StreamReader("TestFile.txt")

        ' Read the stream as a string.
        Dim text As String = Await reader.ReadToEndAsync()

        ' Write the text to the console.
        Console.WriteLine(text)

    End Using
Catch ex As IOException
    Console.WriteLine("The file could not be read:")
    Console.WriteLine(ex.Message)
End Try