VB.NET没有显示文本文件的最后一行

时间:2018-02-27 19:39:34

标签: vb.net file text file-io

我已经在名为Go的vb.net窗体中构建了这个游戏。一旦游戏结束,它将导致匹配细分屏幕。其中一个功能是排行榜。我创建了一个文本文件,用于存储每个玩家的总点数,以便它可以显示在屏幕上。我在游戏结束后通过将新分数附加到文本文件来更新分数。如果玩家是游戏新手,则会创建新记录。所有记录都将显示在表格的排行榜中。

我遇到的问题是当新玩家玩游戏时,会创建一条新记录。但是,它没有显示在排行榜上。我感谢您给我的任何建议,我仍然是VB编程的初学者。以下是代码的摘录:

    filename = "F:\My Go project flood fill2\Text files\leaderboard.txt"
    files = My.Computer.FileSystem.OpenTextFileWriter(filename, True) 'leaderboard
    files.Close()
    Dim leaderboardpath As String = IO.File.ReadAllText(filename)
    Dim lines() As String = IO.File.ReadAllLines(filename)

    ...

    ElseIf leaderboardpath.Contains(handikomi(0, 0)) Then 'If one of the players has a record in text file
        For i As Integer = 0 To lines.Length - 1
            newline = lines(i)
            'append line to new score for black
            Dim currentscore As String
            currentscore = newline.Split(New String() {","}, StringSplitOptions.None)(1).Split(",")(0)
            Dim tempscore As Decimal = currentscore
            currentscore = tempscore + btotalscore
            newline = newline.Split(New String() {","}, StringSplitOptions.None)(0).Split(",")(0) & ", " & currentscore & ", " & newline.Split(New String() {","}, StringSplitOptions.None)(2).Split(",")(0) & ", " & newline.Split(New String() {","}, StringSplitOptions.None)(3).Split(",")(0)
            lines(i) = newline
        Next
        IO.File.WriteAllLines(filename, lines)

        files = New StreamWriter(filename, True) 'leaderboard
        files.WriteLine(handikomi(1, 0) & ", " & wtotalscore & ", 0, 1") 'Writes new record to the file
        files.Close()

        ...

    EndIf

    ...

    leaderlbl.Text = "Leader Board"
    Dim leaderboard As String
    For i As Integer = 0 To lines.Length - 1
        leaderboard = leaderboard & " | " & lines(i)
        leaderlbl.Text = leaderlbl.Text & vbNewLine & lines(i).Split(New String() {","}, StringSplitOptions.None)(0).Split(",")(0) & "  " & lines(i).Split(New String() {","}, StringSplitOptions.None)(1).Split(",")(0)
    Next

1 个答案:

答案 0 :(得分:1)

您展示的用于编写排行榜的代码段使用lines()变量,但在添加新玩家的分数后,我看不到更新lines()变量的代码。解决此问题的一种快速而肮脏的方法是在向其追加一行后重新读取该文件。只需将新播放器分数添加到以下内容中即可更改片段:

files = New StreamWriter(filename, True) 'leaderboard
files.WriteLine(handikomi(1, 0) & ", " & wtotalscore & ", 0, 1") 'Writes new record to the file
files.Close()
lines() As String = IO.File.ReadAllLines(filename) 'Update lines()