VB.NET Read Text File – ReadAllText, Encoding, Async (2026)

With File.ReadAllText you can read a text file in VB.NET in just a few lines. This post covers the simple variant, reading with UTF-8 encoding, and the asynchronous version available since .NET 5 – with complete example code.

Need this for your project?

VB.NET development for your project?

I work with VB.NET and C# every day. Whether file operations or full applications – just reach out.

Code – Read a Text File in VB.NET

Required Class(es)

First, let’s look at which existing classes from the Microsoft .NET Framework we can use. In the framework documentation, we find the File class, which – as expected – allows us to perform file-based operations.

Required Method(s)

The methods we need for our example can be found under the term „Read“:

ReadAllText(path)

Our first and simplest method is ReadAllText, which opens a text file, reads its content, and then closes the file again.

ReadAllText(path, encoding)

Same as the method above, but here we can specify an Encoding to ensure special characters like umlauts are displayed correctly.

ReadAllTextAsync(path, cancellationToken)

Returns the entire content of the text file as a string. This is particularly useful for large files where you don’t want to block the UI thread.

Note: This method is only available in .NET 5+ applications, not in .NET Framework apps.

Let’s Go – Read a Text File in VB.NET

To start, create a simple UI with 2 labels, 2 textboxes, and a button like this:

VB.NET Read Text File GUI
VB.NET Read Text File GUI

Once the controls are properly named, we can start with the button’s code:

Private Sub btnReadFile_Click(sender As Object, e As EventArgs) Handles btnReadFile.Click
    Dim filepath = tbFilepath.Text.Trim()
    If Not File.Exists(filepath) Then
        MessageBox.Show("Please provide an existing filepath..")
        Return
    End If
    ReadFileSyncSample(filepath)
End Sub

First, we grab the file path from the textbox and use Trim() to remove any leading or trailing whitespace.

Then we check if the specified file path exists and immediately exit the sub – following the early return principle – if it doesn’t.

After that, we call the reading method.

The Reading Process

Simple

Here is a simple example of how to read a text file in VB.NET and display it in our tbContent textbox:

Private Sub ReadFileSyncSample(filepath As String)
    Dim contents = File.ReadAllText(filepath)
    tbContent.Text = contents
End Sub

With Encoding

Alternatively, we can specify an encoding for the reading process to ensure special characters like umlauts are displayed correctly:

Private Sub ReadFileSyncSampleWithEncoding(filepath As String)
    Dim encoding = System.Text.Encoding.UTF8
    Dim contents = File.ReadAllText(filepath, encoding)
    tbContent.Text = contents
End Sub

Asynchronous Since .NET 5

Finally, since .NET 5 there is also an asynchronous variant, which is particularly beneficial for file operations with large files:

Private Async Function ReadFileAsyncSample(filepath As String) As Task
    Dim contents = Await File.ReadAllTextAsync(filepath)
    tbContent.Text = contents
End Function

Want to write text files as well? Check out VB.NET write text file.

FAQ

How do I read a text file in VB.NET?

Use File.ReadAllText(path) from the System.IO namespace. It opens the file, reads the entire content as a string, and closes the file automatically.

How do I read a text file with UTF-8 encoding in VB.NET?

Pass the encoding as a second parameter: File.ReadAllText(path, System.Text.Encoding.UTF8). This ensures special characters like umlauts are displayed correctly.

Is there an async method for reading text files in VB.NET?

Yes. Since .NET 5 you can use Await File.ReadAllTextAsync(path). This is especially useful for large files where you don’t want to block the main thread.

Do I need to manually close the file after reading?

No. ReadAllText and ReadAllTextAsync handle opening and closing the file internally. You don’t need a Using block or manual Close() call.

Complete Code – Read a Text File in VB.NET

Imports System.IO

Public Class Form1

    Private Sub btnReadFile_Click(sender As Object, e As EventArgs) Handles btnReadFile.Click
        Dim filepath = tbFilepath.Text.Trim()
        If Not File.Exists(filepath) Then
            MessageBox.Show("Please provide an existing filepath..")
            Return
        End If
        ReadFileSyncSample(filepath)
        ' ReadFileSyncSampleWithEncoding(filepath)
        ' ReadFileAsyncSample()
    End Sub

    Private Sub ReadFileSyncSample(filepath As String)
        Dim contents = File.ReadAllText(filepath)
        tbContent.Text = contents
    End Sub

    Private Sub ReadFileSyncSampleWithEncoding(filepath As String)
        Dim encoding = System.Text.Encoding.UTF8
        Dim contents = File.ReadAllText(filepath, encoding)
        tbContent.Text = contents
    End Sub

    Private Async Function ReadFileAsyncSample(filepath As String) As Task
        Dim contents = Await File.ReadAllTextAsync(filepath)
        tbContent.Text = contents
    End Function

End Class

Downloads

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

Robert Skibbe
Datenschutz-Übersicht

Diese Website verwendet Cookies, damit wir dir die bestmögliche Benutzererfahrung bieten können. Cookie-Informationen werden in deinem Browser gespeichert und führen Funktionen aus, wie das Wiedererkennen von dir, wenn du auf unsere Website zurückkehrst, und hilft unserem Team zu verstehen, welche Abschnitte der Website für dich am interessantesten und nützlichsten sind.