显示文本文件中除最后一行以外的所有内容

Displaying all but the last line in a text file

我已经创建了这个名为 Go 的 2 人游戏,并且我已经将游戏的统计数据记录在一个文本文件中。如果相同的 2 位玩家互相对战不止一次,他们的游戏将被记录在下一行的同一个文本文件中。

我已经完成了所有这些,但现在我想要在屏幕上显示之前的比赛,以便玩家可以看到之前比赛的比分。我不想显示最后一行,因为那是刚刚结束的比赛的分数。到目前为止,这是我的代码:

 Dim file As System.IO.StreamWriter
    'Imports the data from previous forms to form 3
    handikomi = Form1.handi_komi
    prisonerB = Form2.prisonerB
    prisonerW = Form2.prisonerW
    bpass = Form2.bpass
    wpass = Form2.wpass
    bstones = Form2.bstones
    wstones = Form2.wstones
    btotalscore = prisonerB + handikomi(0, 2)
    wtotalscore = prisonerW + handikomi(1, 2)

    If btotalscore > wtotalscore Then
        winnerlbl.Text = (handikomi(0, 0) & " IS THE WINNER!")
    ElseIf wtotalscore > btotalscore Then
        winnerlbl.Text = (handikomi(1, 0) & " IS THE WINNER!")
    End If

    file = My.Computer.FileSystem.OpenTextFileWriter("F:\My Go project flood fill2\Text files\" & handikomi(0, 0) & " VS " & handikomi(1, 0) & ".txt", True)
    'Displays statistics from current match
    file.WriteLine("BLACK" & "," & "WHITE" & "," & "Total Score" & "," & btotalscore & "," & wtotalscore & "," & "Prisoners" & "," & prisonerB & "," & prisonerW & "," & "Passes" & "," & bpass & "," & wpass & "," & "Stones" & "," & bstones & "," & wstones)
    file.Close()

    breakdwnlbl.Text = "                    BLACK   WHITE"
    breakdwnlbl.Text = (breakdwnlbl.Text & vbNewLine & "Total Score" & "     " & btotalscore & "          " & wtotalscore)
    breakdwnlbl.Text = (breakdwnlbl.Text & vbNewLine & "Prisoners" & "         " & prisonerB & "          " & prisonerW)
    breakdwnlbl.Text = (breakdwnlbl.Text & vbNewLine & "Passes" & "             " & bpass & "          " & wpass)
    breakdwnlbl.Text = (breakdwnlbl.Text & vbNewLine & "Stones" & "              " & bstones & "          " & wstones)


    Dim data As String       'to hold value of file line 
    Dim filename As String   'declare file
    filename = "F:\My Go project flood fill2\Text files\" & handikomi(0, 0) & " VS " & handikomi(1, 0) & ".txt"  'path to file on system

    FileOpen(1, filename, OpenMode.Input)  'open file for reading
    'try amend
    Do While Not EOF(1)
        data = LineInput(1)
        MsgBox(data)
    Loop

我认为我不应该在这种情况下使用 EOF,但我不知道还能使用什么,因为我还是个初学者。感谢您的帮助!

阅读所有行并跳过最后一行不是更容易吗:

Dim allLines() As String = File.ReadAllLines(filename)
Dim allButLast = allLines.Take(allLines.Length - 1)

变量 allButLast 是一个 IEnumerable(Of String) 包含文件的所有行,最后一行除外。

更新:

下面是一个显示 MessageBox 中每一行的示例:

For Each line As String In allButLast
    MessageBox.Show(line)
Next