Saving/opening errors
Hello there
I have two questions, though one is only small.
1).
I have been using VB for a few days now and the program i have been making works well. i decided to put a menu bar in to save open etc. but i'm having difficulties with the code. the open/save dialog windows open well but it doesn't save/open here are the codes: to open the savefiledialog:
Code Snippet
SaveFileDialog1.InitialDirectory = "C:\"
SaveFileDialog1.Title = "Save a Text File"
SaveFileDialog1.Filter = "Text Files(*.txt)|*.txt"
SaveFileDialog1.FileName = ""
SaveFileDialog1.OverwritePrompt = True
to open the openfiledialog:
Code Snippet
OpenFileDialog1.InitialDirectory = "C:\"
OpenFileDialog1.Title = "Open a Text File"
OpenFileDialog1.Filter = "Text Files(*.txt)|*.txt"
OpenFileDialog1.FileName = ""
OpenFileDialog1.ShowDialog()
these codes work but it's the follow codes that i get errors in:
saving:
Code Snippet
Dim FILE_NAME As String = SaveFileDialog1.FileName
Dim i As Integer
Dim aryText(4) As String
aryText(0) = (start.Text)
aryText(1) = (num1.Text)
aryText(2) = (num1.Text)
aryText(3) = (ans.Text)
aryText(4) = (time.Text)
Dim objWriter As New System.IO.StreamWriter(FILE_NAME)
For i = 0 To 4
objWriter.WriteLine(aryText(i))
Next
objWriter.Close()
opening:
Code Snippet
Dim FILE_NAME As String = OpenFileDialog1.FileName
Dim TextLine As String
Dim objReader As New System.IO.StreamReader(FILE_NAME)
Do While objReader.Peek() <> -1
TextLine = TextLine & objReader.ReadLine() & vbNewLine
Loop
Textbox1.Text = TextLine
I am getting errors in the lines
Code Snippet
Dim objReader As New System.IO.StreamReader(FILE_NAME)
and
Code Snippet
Dim objWriter As New System.IO.StreamWriter(FILE_NAME)
the error is:
"A first chance exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll"
Does anybody know the problem(s)?
2).
this is propably a slightly stupid question, but...where should this code actuallygo?should it go before, or after, or some before and some after?
activate this code when the user tries to save:
Code Snippet
savefiledialog1.show
system.io.file.writealltext(savefiledialog1.filename,textbox1.text)
That code will save the contents of textbox1 to the directory form the save dialog
open file dialog:
Code Snippet
opefiledialog1.show
textbox1.text = system.io.file.readalltext(openfiledialog1.filename,textbox1.text)