Adam
Adam

Reputation: 13

Reading a text file bottom to top in Visual basic

I've got a text file with a name and a value next to it e.g.

"Toby", 1

"Sam",4

"Ethan",6

I need a way to read the last 10 lines of the file and then output the data. Any ideas would help.

Upvotes: 0

Views: 525

Answers (1)

JayV
JayV

Reputation: 3271

Here are some options for you. There are more.

    Dim fileContent() As String = File.ReadAllLines("D:\Temp\Sample.txt")

    '''' Option 1

    Dim counter1 As Integer = 1
    For Each name As String In fileContent.Reverse
        Debug.WriteLine(name)
        counter1 += 1

        If (counter1 >= 10) Then
            Exit For
        End If
    Next

    '''' Option 2

    Dim fileContent2 As List(Of String) = fileContent.Reverse.ToList()
    For counter2 As Integer = 0 To 9
        Debug.WriteLine(fileContent2(counter2))
    Next

    '''' Option 3

    For counter3 As Integer = fileContent.Length - 1 To fileContent.Length - 10 Step -1
        Debug.WriteLine(fileContent(counter3))
    Next

Upvotes: 2

Related Questions