While Loop Question

SIMIN

Regular
Joined
Mar 10, 2008
Messages
92
Hi all,
How do you open a text file and use a WHILE loop to read all lines, one by one?
Visual Basic:
Dim MyReader As New StreamReader("C:\File.txt")
While MyReader.ReadLine IsNot Nothing
    'How should I get each line here?
End While
 
I think this should work [not compiler tested]

Visual Basic:
' you'll want to find the correct property name
While MyReader.EOF = False
    ...
    MyReader.ReadLine()
Loop
 
I suppose that begs the question which is faster and more readable?

Visual Basic:
Dim myData as String() = System.IO.File.ReadAllLines(...);

vs.

Visual Basic:
' you'll want to find the correct property name
While MyReader.EOF = False
    ...
    MyReader.ReadLine()
Loop

As for myself, I tend to go with the first one since I think it is more readable and for me I time is generally not a concern when processing files line by line.
 
Last edited:
The former should look a little more like the following:
Visual Basic:
Dim myLines As String() = System.IO.File.ReadAllLines(filename)
(Assuming that we are using .Net 2.0 or higher)
 
Back
Top