Visual Basic .NET Language Read All Contents of a File


Example

To read the contents to a file into a string variable:

Dim fileContents As String = System.IO.File.ReadAllText("filename.txt")

ReadAllText will open the specified file, read data to the end, then close the file.

To read a file, separating it into an array element for each line:

Dim fileLines As String() = System.IO.File.ReadAllLines("filename.txt")

ReadAllLines will open the specified file, read each line of the file into a new index in an array until the end of the file, then close the file.