4

我一直在开发一款街机游戏,就像每一款优秀的街机游戏一样,它都有一个集成的记分牌,这样玩家就可以看到谁的得分更高。我的问题是,每次输入新的得分线时,它都会删除文本文件中的所有先前行。我一直在使用的代码如下:

    If player1 > 25 Then

        objReader.Close()
        MsgBox("O " + jogador1 + " ganhou.")
        tab1.Enabled = False

        Dim wrtScore As String = "C:\Documents and Settings\Joao\My Documents\Visual Studio 2010\Projects\flaghunter\flaghunter\deposito\scores.txt"
        Dim objWriter As New System.IO.StreamWriter(wrtScore)

        wrtScore = wrtScore.Trim()

        objWriter.WriteLine(jogador1 + " " + Str(player1))

        objWriter.Close()


    End If

感谢您的关注和任何帮助。

4

3 回答 3

10

改用File.AppendText

    // This text is always added, making the file longer over time
    // if it is not deleted.
 Using sw As StreamWriter = File.AppendText(path)
        sw.WriteLine("This")
        sw.WriteLine("is Extra")
        sw.WriteLine("Text")
 End Using

如果文件第一次不存在,这将创建文件,如果文件已经存在,则附加文本

于 2012-04-07T17:19:13.863 回答
8

Dim objWriter As New System.IO.StreamWriter(wrtScore, TRUE) 附加到文件:D

于 2012-04-07T17:19:16.890 回答
0

附加到文件的开头,尝试加载内容然后覆盖文件并将内容放在底部。就像是:

Dim sr as New StreamReader("path/to/file.ext")
Dim text as String = sr.ReadToEnd()
sr.Close()
Dim sw as New StreamWriter("path/to/file.ext")
sw.WriteLine("New stuff")
sw.WriteLine("More New Stuff")
sw.WriteLine(text) ' Add original content back
sw.Close()
于 2015-11-11T14:24:54.053 回答