读取和写入文本文件
问题描述:
我需要将两位玩家的分数存储在文本文件中。然后我需要模拟投掷骰子,对掷骰子结果进行计算并修改文本文件中玩家的分数。到目前为止创建文本文件我有下面的代码。任何帮助如何更新滚动骰子后的分数将非常感激。读取和写入文本文件
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim filepath As String = "H:\SomeFileName.txt"
If Not System.IO.File.Exists(filepath) Then
System.IO.File.Create(filepath).Dispose()
End If
Dim ObjFso
Dim StrFileName
Dim ObjFile
StrFileName = "H:\SomeFileName.txt"
ObjFso = CreateObject("Scripting.FileSystemObject")
'Creating a file for writing data
ObjFile = ObjFso.CreateTextFile(StrFileName)
'Writing a string into the file
ObjFile.WriteLine("Player 1")
ObjFile.WriteLine("Player 2.")
'Closing the file
ObjFile.Close()
End Sub
End Class
答
这是VBA方式写文件,在VB.NET使用StreamWriter对象(您已经标记了这个问题的VB.NET,对吧?)
Dim filepath As String = "H:\SomeFileName.txt"
Using sw = new StreamWriter(filepath , false)
sw.WriteLine("Player 1")
sw.WriteLine("Player 2.")
End Using
的StreamWriter创建文件,如果它不” t存在并覆盖或附加到文件内的内容,取决于最后的Append
标志