joa*_*oao 4 vb.net text lines streamwriter
我一直在开发街机游戏,并且每个好的街机游戏都有一个内置的记分牌,玩家可以看到谁得分更高.我的问题是,每次进入新的分数线时,它都会删除文本文件中的所有前一行.我一直在使用的代码如下:
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
Run Code Online (Sandbox Code Playgroud)
感谢您的关注和任何帮助.
请改用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
Run Code Online (Sandbox Code Playgroud)
如果文件第一次不存在,则会创建该文件,如果该文件已存在,则附加文本