Ok so I have a program that can save user's input into a text file and load it back, but whenever I try to open the file and exit without selecting the file I get an error.(if i select the file and open it i don't get any errors).

this is the code that handles text file loading

Private Sub Button4_Click(sender As System.Object, e As System.EventArgs) Handles Button4.Click
        OpenFileDialog1.InitialDirectory = "C:\"
        OpenFileDialog1.Filter = "Text Files ONLY (*.txt) | *.txt"
        OpenFileDialog1.ShowDialog()
        Dim loader As New IO.StreamReader(OpenFileDialog1.FileName)
        TextBox1.Text = loader.ReadLine
        TextBox2.Text = loader.ReadLine
        TextBox3.Text = loader.ReadLine
        TextBox4.Text = loader.ReadLine
        TextBox5.Text = loader.ReadLine
        TextBox6.Text = loader.ReadLine
        TextBox7.Text = loader.ReadLine
        TextBox8.Text = loader.ReadLine
        TextBox9.Text = loader.ReadLine
        TextBox10.Text = loader.ReadLine
        TextBox11.Text = loader.ReadLine
        TextBox12.Text = loader.ReadLine
        TextBox13.Text = loader.ReadLine
        TextBox14.Text = loader.ReadLine
        TextBox16.Text = loader.ReadLine
        TextBox57.Text = loader.ReadLine
        loader.Close()
    End Sub

the error is :"FileNotFoundException was unhandled. Could not find file at xxx"
also I would like to know how to make it so that the initial file name for file saving is today's date.
I do not get any errors when I try to save the file, please help :(

The openfiledialog can return a filename, but doesn't have to as you've figured out by cancelling the dialog. The way you've coded this you expect always to get a filename out of openfiledialog.

If you check here: http://msdn.microsoft.com/en-us/library/system.windows.forms.openfiledialog.aspx you'll find an example in the bottom of the page using

If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
'Some code here
end if

to verify that the dialog hasn't been cancelled or closed before proceeding to work with the file.

Or you can check the length of filename.

If OpenFileDialog1.FileName.Length > 0 Then
    Dim loader As New IO.StreamReader(OpenFileDialog1.FileName)
    TextBox1.Text = loader.ReadLine
    TextBox2.Text = loader.ReadLine
    ...
    loader.Close()
End If

Edited 4 Years Ago by Jx_Man: Code tags

Thanks guys, now to my 2nd question, how do i change the initial filename to today's date?

SaveFileDialog1.InitialDirectory = "C:\"
SaveFileDialog1.FileName = "TODAYS DATE"
SaveFileDialog1.Filter = "Text Files ONLY (*.txt) | *.txt"

i.e today is 18 jan 2012 so i want "TODAYS DATE" to be 18-01-2012 and if the program is run again tomorrow it would be 19-01-2012.

Edited 4 Years Ago by Aphrite: n/a

Try this :

SaveFileDialog1.FileName = Date.Now

using your code i get a new error:
NotSupportedException was unhandled - The given path format is not supported.

Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
        SaveFileDialog1.InitialDirectory = "C:\"
        SaveFileDialog1.FileName = Date.Now
        SaveFileDialog1.Filter = "Text Files ONLY (*.txt) | *.txt"
        SaveFileDialog1.ShowDialog()
        Dim saver As New IO.StreamWriter(SaveFileDialog1.FileName)
        saver.WriteLine(TextBox1.Text)
        saver.WriteLine(TextBox2.Text)
        saver.WriteLine(TextBox3.Text)
        saver.WriteLine(TextBox4.Text)
        saver.WriteLine(TextBox5.Text)
        saver.WriteLine(TextBox6.Text)
        saver.WriteLine(TextBox7.Text)
        saver.WriteLine(TextBox8.Text)
        saver.WriteLine(TextBox9.Text)
        saver.WriteLine(TextBox10.Text)
        saver.WriteLine(TextBox11.Text)
        saver.WriteLine(TextBox12.Text)
        saver.WriteLine(TextBox13.Text)
        saver.WriteLine(TextBox14.Text)
        saver.WriteLine(TextBox16.Text)
        saver.WriteLine(TextBox57.Text)
        saver.Close()
    End Sub

Nevermind I fixed it with this

DateTime.Now.ToString("MM.dd.yyyy")

Thanks again :D

Yes. Good Job..
I not have compiler installed right now. so i forgot that windows cannot save symbols like "/" as an address.
Don't forget to mark this thread as solved.

Edited 4 Years Ago by Jx_Man: n/a

This question has already been answered. Start a new discussion instead.