how to read line by line from a text file from vb.net

sumeet

Member
Joined
Aug 19, 2005
Messages
12
Location
india,mumbai
Programming Experience
Beginner
we have created a program that reads only the first line from textfile but we r not able to read next line.
our codes are as follows:-

Dim fstream As FileStream = New FileStream("D:\john\abc.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite)

Dim br As StreamReader = New StreamReader(fstream)

Dim bw As StreamWriter = New StreamWriter(fstream)

br.BaseStream.Seek(0, SeekOrigin.Begin)

str = br.ReadLine()

TextBox1.Text = str

 
I'm in a hurry so my response may be rushed, but:

br will remember the last line it read. And every time you call br.ReadLine() it will increment to the next line. Thus you can loop br.ReadLine() until it reaches EOF (End Of File).

Welcome to vbdnf
 
could read the entire file into a string variable then use the .Split function to split it into an array (line by line) then just read the next array element when you need the next line

at least that's how i would do it anyways
 
Yeah that is a great way to quickly get the file closed, so you can handle all the data without worrying about freeing the file up. But I'm anal like that.
 
as am i, i'll only have a file open long enough to read/write to it and everything else that i need to do with the file, i'll do it in memory so the file can be used elsewhere on the system
 
Back
Top