vba - 如何将文本附加到文本文件?
问题描述
我想将文本框中的文本写入文本文件。
代码写入文本文件的第一行。当我清除文本框以写入其他值时,之前写入的内容消失并被新值替换。
如何获得要写入前一个值的新值集?
Dim path As String = TextBox1.Text & ".txt"
path = "C:\Users\USER\Desktop\" & path
Dim studentno As Integer = TextBox2.Text
Dim name As String = TextBox3.Text
Dim surname As String = TextBox4.Text
Dim subject As String = TextBox8.Text
Dim indv As Integer = TextBox5.Text
Dim grp As Integer = TextBox6.Text
Dim test As Integer = TextBox7.Text
If System.IO.File.Exists(path) = True Then
Dim sw As New System.IO.StreamWriter(path)
sw.WriteLine(studentno & "," & name & "," & surname & "," & subject & "," & indv & "," & grp & "," & test)
sw.Close()
MessageBox.Show("Text written to file.", "Note", MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
MessageBox.Show("File Does Not Exist.", "Note", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub
解决方案
这是我必须创建文件的代码,它工作得很好,所以这很好。
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim path As String = TextBox1.Text & ".txt"
path = "C:\Users\USER\Desktop\" & path
If String.IsNullOrEmpty(TextBox1.Text) = True Then
MessageBox.Show("Textbox Empty", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error)
ElseIf Not File.Exists(path) Then
File.Create(path).Dispose()
MessageBox.Show("File created.", "Note", MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
MessageBox.Show("File already exists.", "Note", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub
推荐阅读
- r - 访问在函数中创建的 R 对象
- javascript - 如何在第二个参数中获取 serverinfo?
- php - Google Hangout Chat - 向用户发送直接消息
- python - 使用 PySpark 删除 spark 数据框中嵌套结构中的列(文本中的详细信息)
- css - 带填充的下划线:非链接
- javascript - Javascript - 将字符串数组映射到具有多个对象属性的对象数组
- c++ - 自定义聚合初始化列表构造函数
- python - 如何获取 csv 列中出现的计数并保存为包含 python 中的计数的新 csv
- c# - C#:将数据从控制器传递到应用程序层到 DBContext 层
- excel - Excel:从每日替换的 Excel 文件中获取数据