VB.NET offers several ways to read a file. Often, the more old fashioned yet more efficient method of reading a file line by line, is seen as the preferrable way of doing this.
Reading a file using a stream
A stream is what is basically a bunch of bytes that a file system provides to
a subsystem in chunks. VB.NET, like many languages, provides the StreamReader
object to work with file reading.
The following example reads a text file line by line and write the line number of the line and the line contents to the console:
VB.NET
Dim line_no As Integer = 1 Dim line As String Dim reader As StreamReader = New StreamReader("file.txt") line = reader.ReadLine 'Keep looping until line becomes nothing Do Until (line Is Nothing) 'Write the line and line number to console Console.WriteLine(line_no & & line) line_no = line_no + 1 line = reader.ReadLine Loop
Did you know my courses are all printer-friendly? This means you can easily export them to PDF or print them for later use.