Reading text from files
We will continue to utilize the two different methods for VB.NET and older VB family members. For VB.NET, we will use the File
class from the System.IO
namespace to read text from files. For VBScript, VBA, and VB6, we will continue to use the FSO we used in the files and directories section.
The File
class also provides two methods that make writing text to a file in a single line easy. The ReadAllText
and ReadlAllLines
methods pull all the text from the file into a single variable. The difference between the two methods is that the second method pulls the data line by line into an array, while the first does not separate the input file lines:
Imports System.IO Dim myFile As String = "C:\Path\File.txt" Dim myText As String = File.ReadAllText(myFile)
In VB.NET, the StreamReader
class allows you to read text from a file. First, you create a StreamReader
instance, and then use its Read
or ReadLine
methods to read the text:
Imports System.IO...