Home
VB.NET
StreamReader Example
Updated Mar 2, 2025
Dot Net Perls
StreamReader. This VB.NET class handles text files. We read a text file by Using StreamReader. This will correctly dispose of system resources and make code simpler.
The best way to use StreamReader requires some special syntax. The Using-keyword allows you to automate disposal of the system resources, which is critical for performance and stability.
StreamWriter
File
Example. The example reads the first line from file.txt in the local directory. You need to add a text file called "file.txt" and include it in the build directory ("Copy if newer").
Start Look at how the Using-statement is formed to create the StreamReader. This is the most common usage pattern for StreamReader.
Also We include the System.IO namespace at the top, with the line "Imports System.IO". This is important to compile the program.
Imports System.IO

Module Module1

    Sub Main()
        ' Store the line in this String.
        Dim line As String

        ' Create new StreamReader instance with Using block.
        Using reader As StreamReader = New StreamReader("file.txt")
            ' Read one line from file
            line = reader.ReadLine
        End Using

        ' Write the line we read from "file.txt"
        Console.WriteLine(line)
    End Sub

End Module
(Contents of file.)
Example 2. This example starts off by declaring a new List(Of String). This instance is a generic List. The Using-statement is used next, and it opens the "file.txt" file.
Also There is a local line variable declared, which will store each line as it is read.
List
Info Before the loop begins, we read the first line. Then we enter a Do While loop, which continues until the last line read is Nothing.
Detail At the end of the program, I inserted a debugger breakpoint to show the contents of the List.
And I found that there were two string elements, containing Line 1 and Line 2 of the text file we read.
Imports System.IO

Module Module1

    Sub Main()
        ' We need to read into this List.
        Dim list As New List(Of String)

        ' Open file.txt with the Using statement.
        Using r As StreamReader = New StreamReader("file.txt")
            ' Store contents in this String.
            Dim line As String

            ' Read first line.
            line = r.ReadLine

            ' Loop over each line in file, While list is Not Nothing.
            Do While (Not line Is Nothing)
                ' Add this line to list.
                list.Add(line)
                ' Display to console.
                Console.WriteLine(line)
                ' Read in the next line.
                line = r.ReadLine
            Loop
        End Using

    End Sub

End Module
It is possible to use the Using-statement in VB.NET, along with StreamReader, to read in lines from text files. In most programs StreamReader is well-performing.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Mar 2, 2025 (edit).
Home
Changes
© 2007-2025 Sam Allen