我正在尝试创建一个迷你帐户应用程序,通过文本框捕获并存储学生的名字,姓氏,学号和成绩,然后将其存储到逗号分隔的文本文件中。问题是每次我点击保存文件时它都会重写输入。任何帮助,将不胜感激。这是我正在使用的代码:
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
Dim fName, lName, sNum As String
Dim grade As Decimal
fName = txtFName.Text
lName = txtLName.Text
sNum = txtSNum.Text
grade = txtGrade.Text
Try
Dim write As StreamWriter
write = New StreamWriter("hello.txt")
write.WriteLine(String.Format("{0},{1},{2},{3}", fName, lName, sNum, grade))
write.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
答案 0 :(得分:0)
尝试将其更改为
Private Sub btnSave_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSave.Click
Dim fName, lName, sNum As String
Dim grade As Decimal
fName = txtFName.Text
lName = txtLName.Text
sNum = txtSNum.Text
grade = txtGrade.Text
Try
System.IO.File.WriteAllText("hello.txt", String.Format("{0},{1},{2},{3}", fName, lName, sNum, grade))
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
答案 1 :(得分:0)
您也可以尝试这样做:它首先读取内容并在写入时附加此内容
Try
Dim objReader = New StreamReader(FullPath)
Dim str As String = objReader.ReadToEnd()
objReader.Close()
Dim write As StreamWriter
write = New StreamWriter(FullPath)
write.Write(str & vbNewLine & String.Format("{0},{1},{2},{3}", fName, lName, sNum, grade))
write.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try