Home
VB.NET
StreamReader ReadToEnd Function
Updated Mar 24, 2022
Dot Net Perls
StreamReader, ReadToEnd. When reading files, we often need to get the entire file into a string. Many Functions in VB.NET can be used for this, including File.ReadAllText.
But with StreamReader ReadToEnd, we have another option. If we are already using StreamReader in the program, ReadToEnd is a particularly good choice.
StreamReader
An example. We use the ReadToEnd method on a StreamReader, instance, so we should import System.IO. A Using-statement is the best way to use StreamReader.
And We assign a String variable to the contents of the return value of ReadToEnd.
Imports System.IO Module Module1 Sub Main() ' Wrap StreamReader in using block. Using streamReader As StreamReader = New StreamReader("C:\programs\file.txt") ' Get entire contents of file in string. Dim contents As String = streamReader.ReadToEnd() Console.WriteLine(contents) End Using End Sub End Module
Thank you Friend
Notes. Some kinds of files, like CSV files, are best parsed with ReadLine. But for other kinds of files, like data files, ReadToEnd is a better choice.
A summary. In a recent project, ReadToEnd was valuable to me. It allowed the program to maintain more symmetry, a benefit over a Function like File.ReadAllText.
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 24, 2022 (edit link).
Home
Changes
© 2007-2025 Sam Allen