Hello and thank you in advance everyone.
I am new to the forum and to vb.net, I am trying to use filestream and streamwriter / streamreader to have a simple text box where the text entered is written to a text file, and then a button can be clicked to read the file and present the text back to the textbox. It works, but the problem I'm having is that if i change the text to something smaller in length (less text) than what is currently in the file, then press the button to read the file, it still contains some of the old text that was previously in the file.
I can only presume that I am not clearing something out properly, but I may be wrong. Here below is how I have it coded. Can anyone help please?
here is the reader portion
Dim strmReadFile As New FileStream(txtPath.Text, FileMode.Open, FileAccess.Read, FileShare.Read)
Dim readHandle As New StreamReader(strmReadFile)
'read the entire text, and set it to a string
Dim streamFileContents As String = readHandle.ReadToEnd()
'put the files contents out to the textbox
txtNotes.Text = streamFileContents
'close stream and reader
strmReadFile.Close()
readHandle.Close()
below is the writer portion
Dim strmWriteFile As New FileStream(txtPath.Text, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read)
Dim writeHandle As New StreamWriter(strmWriteFile)
writeHandle.WriteLine(txtNotes.Text)
writeHandle.Flush()
'close writer and stream
writeHandle.Close()
strmWriteFile.Close()
I am new to the forum and to vb.net, I am trying to use filestream and streamwriter / streamreader to have a simple text box where the text entered is written to a text file, and then a button can be clicked to read the file and present the text back to the textbox. It works, but the problem I'm having is that if i change the text to something smaller in length (less text) than what is currently in the file, then press the button to read the file, it still contains some of the old text that was previously in the file.
I can only presume that I am not clearing something out properly, but I may be wrong. Here below is how I have it coded. Can anyone help please?
here is the reader portion
Dim strmReadFile As New FileStream(txtPath.Text, FileMode.Open, FileAccess.Read, FileShare.Read)
Dim readHandle As New StreamReader(strmReadFile)
'read the entire text, and set it to a string
Dim streamFileContents As String = readHandle.ReadToEnd()
'put the files contents out to the textbox
txtNotes.Text = streamFileContents
'close stream and reader
strmReadFile.Close()
readHandle.Close()
below is the writer portion
Dim strmWriteFile As New FileStream(txtPath.Text, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read)
Dim writeHandle As New StreamWriter(strmWriteFile)
writeHandle.WriteLine(txtNotes.Text)
writeHandle.Flush()
'close writer and stream
writeHandle.Close()
strmWriteFile.Close()