SIMIN Posted August 26, 2008 Posted August 26, 2008 Hi all, How do you open a text file and use a WHILE loop to read all lines, one by one? Dim MyReader As New StreamReader("C:\File.txt") While MyReader.ReadLine IsNot Nothing 'How should I get each line here? End While Quote
Nate Bross Posted August 26, 2008 Posted August 26, 2008 I think this should work [not compiler tested] ' you'll want to find the correct property name While MyReader.EOF = False ... MyReader.ReadLine() Loop Quote ~Nate� ___________________________________________ Please use the [vb]/[cs] tags on posted code. Please post solutions you find somewhere else. Follow me on Twitter here.
Leaders snarfblam Posted August 27, 2008 Leaders Posted August 27, 2008 If it helps, there is also the System.IO.File.ReadAllLines() method. Quote [sIGPIC]e[/sIGPIC]
Nate Bross Posted August 28, 2008 Posted August 28, 2008 (edited) I suppose that begs the question which is faster and more readable? Dim myData as String() = System.IO.File.ReadAllLines(...); vs. ' 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. Edited August 28, 2008 by Nate Bross Quote ~Nate� ___________________________________________ Please use the [vb]/[cs] tags on posted code. Please post solutions you find somewhere else. Follow me on Twitter here.
Leaders snarfblam Posted August 28, 2008 Leaders Posted August 28, 2008 The former should look a little more like the following: Dim myLines As String() = System.IO.File.ReadAllLines(filename) (Assuming that we are using .Net 2.0 or higher) Quote [sIGPIC]e[/sIGPIC]
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.