E
Eric A. Johnson
I'm trying to create a text file, write to it, close it, then reopen the
same file for reading (line by line). I've so far been able to successfully
open the file, write to it, then close it. I let the user decide which file
to write to, using the following code:
' File writing/reading variables
Dim SaveFileResult As SaveFileDialog
Dim TextFileStream As FileStream
Dim TextStreamWriter As StreamWriter
Dim TextStreamReader As StreamReader
SaveFileResult = New SaveFileDialog
SaveFileResult.Filter = "Text Files|*.txt|All Files|*.*"
SaveFileResult.FilterIndex = 0
Dim FileResult As DialogResult
FileResult = SaveFileResult.ShowDialog
If FileResult = DialogResult.OK Then
' Open file for writing
TextFileStream = SaveFileResult.OpenFile
TextStreamWriter = New StreamWriter(TextFileStream)
Else ' Since they cancelled, exit sub
txtText.Focus()
txtText.Select(0, txtText.Text.Length)
Exit Sub
End If
End If
.... This works successfully. Is there a way I can then change it from
write-only access to read-only access, or would I need to close it first?
Also, how can I open it with the same filename? I'm really new to working
with files, so any help would be appreciated. Thank you very much!
same file for reading (line by line). I've so far been able to successfully
open the file, write to it, then close it. I let the user decide which file
to write to, using the following code:
' File writing/reading variables
Dim SaveFileResult As SaveFileDialog
Dim TextFileStream As FileStream
Dim TextStreamWriter As StreamWriter
Dim TextStreamReader As StreamReader
SaveFileResult = New SaveFileDialog
SaveFileResult.Filter = "Text Files|*.txt|All Files|*.*"
SaveFileResult.FilterIndex = 0
Dim FileResult As DialogResult
FileResult = SaveFileResult.ShowDialog
If FileResult = DialogResult.OK Then
' Open file for writing
TextFileStream = SaveFileResult.OpenFile
TextStreamWriter = New StreamWriter(TextFileStream)
Else ' Since they cancelled, exit sub
txtText.Focus()
txtText.Select(0, txtText.Text.Length)
Exit Sub
End If
End If
.... This works successfully. Is there a way I can then change it from
write-only access to read-only access, or would I need to close it first?
Also, how can I open it with the same filename? I'm really new to working
with files, so any help would be appreciated. Thank you very much!