Question Retrieving more than one line out of a text file?

fenhopi

Member
Joined
Jul 8, 2010
Messages
6
Programming Experience
Beginner
Hi!
I'm trying to retrieve line number 5 - 8 from a text file, but I can't figure out how. I can retrieve a single line and I can retrieve all lines, but I can't figure out how to get those specific ones. I tried using substring but it didn't work. Any ideas?
Thank you in advance!
 
If you're using StreamReader and ReadLine method you can read first 4 lines without storing/processing them, then read the next 4 lines that you process.
If you use ReadAllLines it returns an array of all lines in file and you can access array elements 4-7 (zero based).
 
Okay, interesting. I'm already using readallLines.
This is what I use to get a word that's on the same line as what I searched for:
Dim test = FindStringInFile("\filepath", TextBox1.Text.Substring(0, 8))
If test Then
For Each line In IO.File.ReadAllLines("filepath")
'Line = linjenummer i vare.txt
'Vist linjenummeret inneholder scanummeret i textbox1, hent varenummer
If line.Contains(TextBox1.Text.Substring(0, 8)) Then
Label2.Text = line.Substring(3, 6)
Exit For
End If
Next
Could you please provide an example on how I would put the information in the next 5 lines under line.Substring(3, 6) into a label?

Thank you for your time.
 
Use a For-Next loop instead, when you find a line match you have the index and can retrieve the next 5 index directly.
 
Back
Top