我一直在开发街机游戏,并且每个好的街机游戏都有一个合并的记分牌,以便玩家可以看到谁得分更高。我的问题是,每次进入新的分数线时,它都会删除文本文件中的所有前一行。我一直在使用的代码如下:
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
感谢您的关注和任何帮助。
答案 0 :(得分:8)
改为使用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
如果文件第一次不存在,则会创建该文件,如果该文件已存在则附加文本
答案 1 :(得分:7)
Dim objWriter As New System.IO.StreamWriter(wrtScore, TRUE)
附加到文件:D
答案 2 :(得分: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()