在 Visual basic 中从下到上读取文本文件

Reading a text file bottom to top in Visual basic

我有一个文本文件,旁边有一个名称和一个值,例如

“托比”,1

“山姆”,4

“伊桑”,6

我需要一种方法来读取文件的最后 10 行,然后输出数据。任何想法都会有所帮助。

这里有一些选项供您选择。还有更多。

    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