Reading Datafile in As Few Lines As Necessary

Stephen Woehr 0 Reputation points
2026-08-03T18:21:58.1066667+00:00

I know the DataReader method, but I thought there was a way using "ReadLine" or something like that.

Developer technologies | VB
0 comments No comments

3 answers

Sort by: Most helpful
  1. Senthil kumar 1,730 Reputation points
    2026-08-08T06:37:34.38+00:00

    Hi @Stephen Woehr

     Dim lstData As List(Of String) = File.ReadAllLines("d:\data\datafile.txt").Take(10).ToList()
    
    
     lstData.ForEach(Function(x)
                         Console.WriteLine(x)
                     End Function)
    

    this will data load to list.

    Thanks.

    Was this answer helpful?

    0 comments No comments

  2. Jay Pham (WICLOUD CORPORATION) 3,905 Reputation points Microsoft External Staff Moderator
    2026-08-04T00:28:05.08+00:00

    Hi @Stephen Woehr ,

    Adding a concrete VB example on top of what was already mentioned. File.ReadLines is the one you are looking for. It returns an IEnumerable(Of String), so lines are pulled only as you enumerate them.

    Imports System.IO
    
    ' Read only the first 10 lines, nothing beyond that is read from disk
    For Each line As String In File.ReadLines("C:\data\myfile.txt").Take(10)
        Console.WriteLine(line)
    Next
    
    ' Or stop on a condition
    For Each line As String In File.ReadLines("C:\data\myfile.txt")
        If line.StartsWith("END") Then Exit For
        ' process line
    Next
    

    If you prefer controlling each read yourself, StreamReader.ReadLine() does the same job.

    Using reader As New StreamReader("C:\data\myfile.txt")
        Dim line As String = reader.ReadLine()
        Do While line IsNot Nothing
            ' process line
            line = reader.ReadLine()
        Loop
    End Using
    

    The key difference to watch for: File.ReadAllLines loads the entire file into memory before you can touch the first line, so that is the one to avoid for your scenario.

    I hope this helps! If my answer was helpful so far, you can follow this guidance to provide some feedback. This also help others find the solution easier.  

    Was this answer helpful?


  3. AI answer

    2026-08-03T18:22:10.99+00:00

    DataReader is for reading rows from a data source such as a database. It returns an unbuffered, forward-only stream, and rows are read by calling ExecuteReader() and then Read().

    If the goal is to read a text file line by line in VB, LineInput exists, but it is provided for backward compatibility and may affect performance. For non-legacy applications, My.Computer.FileSystem provides better performance.

    If the goal is to read only as much of a file as needed, File.ReadLines is the better fit. It returns the lines of a file as IEnumerable<string>, and enumeration can begin before the whole collection is returned. That makes it more efficient than reading the entire file first when working with large files.


    AI-generated content may be incorrect. Read our transparency notes for more information.

    Was this answer helpful?

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.