File.ReadAllTextThis function reads an entire text file. It returns a String. We store this data into a String dim in our VB.NET program.
File.ReadAllText is found in the System.IO namespace. As with all file handling Functions, we must be alert to possible exceptions.
To start, this program imports the System.IO namespace. In Main, it calls the File.ReadAllText function and passes the String literal "C:\file.txt" as the parameter.
String data returned by the File.ReadAllText function is stored in the Dim value. We get a string from a file.String data is printed to the screen with the Console.WriteLine subroutine.Imports System.IO
Module Module1
Sub Main()
' Open file and store in String.
Dim value As String = File.ReadAllText("C:\file.txt")
' Write to screen.
Console.WriteLine(value)
End Sub
End ModuleFile contents.What should you do if you want to read in a file line-by-line instead of storing the entire content in a String? The StreamReader type is ideal for this purpose.
ReadLine on StreamReader. This can lead to performance improvements over File.ReadAllText.The File.ReadAllText function reads the entire text contents into a String. StreamReader can read files in line-by-line, which is more efficient with large files.