Hello. I am writing a program that will read and write to a file using the My.Computer.Filesystem namespace, and when I append my data file with the code shown below under SaveFile subroutine, it sometimes places extra ControlChars.Newline at the end of the appended text. Sometimes it places one extra newline, and sometimes it adds 3-6 newlines. I have tried everything I can think of to make it add just one newline at the end of each append, as I will be using the newline char to flag each entry on retrieval. I am working out some algorithms in this project to use in a project that will keep track of data in a file using csv format(just practicing saving/retrieving data from a file). The extra newline characters that are added will become a problem when I start to sort the data to display. Any help will be appreciated.
my xSaveButton click event:
my save subroutine:Code:Private Sub xSaveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles xSaveButton.Click Dim boolAppendData As Boolean If My.Computer.FileSystem.FileExists("myData.dat") = True Then Dim overWrite As String = MsgBox("Overwrite data file? Selecting yes will overwrite" & vbCrLf & _ "the data file. Selecting no will append to it.", MsgBoxStyle.YesNo, "Data File") If overWrite = DialogResult.Yes Then boolAppendData = False Else boolAppendData = True End If SaveFile(boolAppendData) Else SaveFile(False) End If End Sub
Code:Private Sub SaveFile(ByVal boolAppend As Boolean) If Me.xTextBox.Text = String.Empty Then MsgBox("There is nothing to save to the file." & vbCrLf & "Type something in the textbox " _ & vbCrLf & "or open a file first.", MsgBoxStyle.OkOnly, "Empty Textbox") Else Try My.Computer.FileSystem.WriteAllText("myData.dat", Me.xTextBox.Text & ControlChars.NewLine, boolAppend) MsgBox("File saved successfully.", MsgBoxStyle.OkOnly, "File Saved") Catch ex As Exception MsgBox("There was a problem saving the file." & vbCrLf & ex.ToString, MsgBoxStyle.OkOnly, "File Not Saved") End Try End If End Sub







Reply With Quote