How to Extract Text from Text File 1 Line at a Time

I'm making a program that will need to open a simple text file, start at line 1, extract line 1 and save it as a string, execute a block of code with that string, and then move onto the next line and do the same thing.

I've looked at other topics, but I need something that doesn't just throw all of the data into a list box; it needs to temporarily save the line to a string until the next time when the string is rewritten.

To make things simple, perhaps anyone who knows how to make a program that has a textbox and a button on the form can help. When the program starts, it extracts the first line in a text file on the hard drive, displays it in the textbox, and then waits for the user to press the button, at which point it starts the process over again, after advancing 1 line in the text document.

Thanks for any help.



Answer this question

How to Extract Text from Text File 1 Line at a Time

  • Mike Greiner

    This code might help:

    Imports System.IO
    Public Class Form1
    Private fs As StreamReader

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    If Not fs.EndOfStream Then TextBox1.Text = fs.ReadLine
    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    fs = New StreamReader("c:\temp\test.txt")
    End Sub
    End Class



  • Kevin Berry

    To extract a line at a time to process you can do in a number of ways. Here are some examples which read in the lines in a file and add them into an array or arraylist which can be processed.

    You can modify this to either process them instead of adding them to an array structure. Or read them into the array structure and then process this.

    Three Examples


    '//TextFieldParser
    Dim x1 As New ArrayList
    Dim reader As Microsoft.VisualBasic.FileIO.TextFieldParser
    reader = My.Computer.FileSystem.OpenTextFieldParser _
    ("c:\Output.txt")
    reader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited
    Dim i As Integer = 1
    While Not reader.EndOfData
    Try
    x1.Add(reader.ReadLine)
    Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
    MsgBox("Line " & ex.Message & _
    "is not valid and will be skipped.")
    End Try
    End While
    MsgBox(x1.Count)

    '//Split into array - Read String and Split
    Dim s As String = My.Computer.FileSystem.ReadAllText("C:\Output.txt")
    Dim x2 As String() = s.Split(vbCr)
    MsgBox(x2.Length)

    '//Streamreader
    Dim x3 As New ArrayList
    Dim fileReader As System.IO.StreamReader
    fileReader = My.Computer.FileSystem.OpenTextFileReader("C:\Output.txt")
    While Not fileReader.EndOfStream
    x3.Add(fileReader.ReadLine)
    End While
    MsgBox(x3.Count)


  • Cesar Francisco

    How could this be applied to reading a single line from a text file on the internet Can this work

    Code Block

    Dim strURLPathFile

    Dim strLineTextRead

    Dim fs As StreamReader

    strURLPathFile = "http://www.myserver.com/myfolder/myfile.txt

    fs = New StreamReader("strURLPathFile")

    If Not fs.EndOfStream Then strLineTextRead = fs.ReadLine

    Well, I've tried it and it didn't work. How can a URL be used as part of the file path



  • How to Extract Text from Text File 1 Line at a Time