如何在文本文件中的行旁边附加新的字符串(数据)行和新的 "index number"
How do i append a new row of strings(data) with a new "index number" next to the row in a text file
我想使用流编写器或vb.net中的任何方法将新行和新索引号附加到文本文件(我使用的是 2015 版本)
我设法让它保存,但它只保存第一个文本框。
这是我目前所拥有的:
Dim outputFile As IO.StreamWriter
Dim filePath As String = "PCPT2Database.txt"
Dim intNewNo() As Integer = {}
Dim intCount As Integer = 0
Dim currentRow() As String
Dim intLoop As Integer
Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser(filePath)
MyReader.TextFieldType = FileIO.FieldType.Delimited
MyReader.SetDelimiters(",")
While Not MyReader.EndOfData
Try
currentRow = MyReader.ReadFields()
ReDim Preserve intNewNo(intCount)
intNewNo(intCount) = currentRow(0)
intCount = +1
Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
MsgBox("line" & ex.Message & " Its not valid.")
End Try
End While
End Using
For intLoop = 0 To intCount + 1
Next
If IO.File.Exists(filePath) = True Then
outputFile = IO.File.AppendText(filePath)
outputFile.WriteLine(intNewNo(intLoop), txtName.Text, txtSurname.Text, mtbCell.Text, txtEmail.Text)
'intNewNo(intLoop),
MessageBox.Show("Save Successful")
outputFile.Close()
MessageBox.Show("Add another record", "Cancel", MessageBoxButtons.OKCancel, MessageBoxIcon.Information)
txtName.Clear()
txtSurname.Clear()
mtbCell.Clear()
txtEmail.Clear()
Else
MessageBox.Show(filePath, ".txt not found.", _
MessageBoxButtons.RetryCancel, MessageBoxIcon.Information)
End If
End Sub
删除代码顶部的 outputFile
声明并改为使用以下内容:
If IO.File.Exists(filePath) Then
Using outputFile = IO.File.AppendText(filePath)
outputFile.WriteLine(Strings.Join({intNewNo(intLoop).ToString, txtName.Text, txtSurname.Text, mtbCell.Text, txtEmail.Text}, ","))
End Using
MessageBox.Show("Save Successful")
MessageBox.Show("Add another record", "Cancel", MessageBoxButtons.OKCancel, MessageBoxIcon.Information)
txtName.Clear()
txtSurname.Clear()
mtbCell.Clear()
txtEmail.Clear()
Else
MessageBox.Show(filePath, ".txt not found.",
MessageBoxButtons.RetryCancel, MessageBoxIcon.Information)
End If
但是您的代码需要优化(有点?)(:
我想使用流编写器或vb.net中的任何方法将新行和新索引号附加到文本文件(我使用的是 2015 版本) 我设法让它保存,但它只保存第一个文本框。 这是我目前所拥有的:
Dim outputFile As IO.StreamWriter
Dim filePath As String = "PCPT2Database.txt"
Dim intNewNo() As Integer = {}
Dim intCount As Integer = 0
Dim currentRow() As String
Dim intLoop As Integer
Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser(filePath)
MyReader.TextFieldType = FileIO.FieldType.Delimited
MyReader.SetDelimiters(",")
While Not MyReader.EndOfData
Try
currentRow = MyReader.ReadFields()
ReDim Preserve intNewNo(intCount)
intNewNo(intCount) = currentRow(0)
intCount = +1
Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
MsgBox("line" & ex.Message & " Its not valid.")
End Try
End While
End Using
For intLoop = 0 To intCount + 1
Next
If IO.File.Exists(filePath) = True Then
outputFile = IO.File.AppendText(filePath)
outputFile.WriteLine(intNewNo(intLoop), txtName.Text, txtSurname.Text, mtbCell.Text, txtEmail.Text)
'intNewNo(intLoop),
MessageBox.Show("Save Successful")
outputFile.Close()
MessageBox.Show("Add another record", "Cancel", MessageBoxButtons.OKCancel, MessageBoxIcon.Information)
txtName.Clear()
txtSurname.Clear()
mtbCell.Clear()
txtEmail.Clear()
Else
MessageBox.Show(filePath, ".txt not found.", _
MessageBoxButtons.RetryCancel, MessageBoxIcon.Information)
End If
End Sub
删除代码顶部的 outputFile
声明并改为使用以下内容:
If IO.File.Exists(filePath) Then
Using outputFile = IO.File.AppendText(filePath)
outputFile.WriteLine(Strings.Join({intNewNo(intLoop).ToString, txtName.Text, txtSurname.Text, mtbCell.Text, txtEmail.Text}, ","))
End Using
MessageBox.Show("Save Successful")
MessageBox.Show("Add another record", "Cancel", MessageBoxButtons.OKCancel, MessageBoxIcon.Information)
txtName.Clear()
txtSurname.Clear()
mtbCell.Clear()
txtEmail.Clear()
Else
MessageBox.Show(filePath, ".txt not found.",
MessageBoxButtons.RetryCancel, MessageBoxIcon.Information)
End If
但是您的代码需要优化(有点?)(: