我需要将两个玩家的分数存储在一个文本文件中。然后我需要模拟投掷骰子,对掷骰子的结果进行计算并修改文本文件中玩家的分数。到目前为止创建文本文件我有下面的代码。任何有关如何在掷骰子后更新分数的帮助都将非常感激。
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
答案 0 :(得分:1)
那是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会创建该文件,并根据最后Append
标志覆盖或附加到文件内的内容