Click here to Skip to main content
15,071,262 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
HI,
i am reading the contents of textfile to vb.the textfile has around 100 lines,but i want to read only first 10 lines how to do it.

Dim TextBox1 As String : TextBox1 = "D:\temp.txt"
Dim lines1() As String = System.IO.File.ReadAllLines(TextBox1)

i am able to read full txt file contents,but i need only 10 lines of it.

What I have tried:

Dim TextBox1 As String : TextBox1 = "D:\temp.txt"
Dim lines1() As String = System.IO.File.ReadAllLines(TextBox1)

i am able to read full txt file contents,but i need only 10 lines of it.
Posted
Updated 15-May-17 0:18am

Iteratively use the StreamReader.ReadLine[^] method.
   
Comments
Maciej Los 15-May-17 6:19am
   
5ed!
CPallini 16-May-17 13:24pm
   
Thank you, Maciej!
c# - How do I read a specified line in a text file? - Stack Overflow[^]

sample code
Dim line As String = File.ReadLines(FileName).Take(10).First()

Dim line As String = File.ReadLines(FileName).Skip(14).Take(1).First()
   
v2
Comments
Member 12659926 15-May-17 7:07am
   
Thanks a lot
sameer549 15-May-17 7:39am
   
welcome, please always check google before posting question.
If you want to read 10 first lines only, please use this:
Dim lines = File.ReadLines(FileName).Take(10).ToList() 'returns List(Of String)
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900