Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

Posted

I think this should work [not compiler tested]

 

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

~Nate�

___________________________________________

Please use the [vb]/[cs] tags on posted code.

Please post solutions you find somewhere else.

Follow me on Twitter here.

Posted (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 by Nate Bross

~Nate�

___________________________________________

Please use the [vb]/[cs] tags on posted code.

Please post solutions you find somewhere else.

Follow me on Twitter here.

  • Leaders
Posted

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)

[sIGPIC]e[/sIGPIC]

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...