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"
        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
    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 :(

Recommended Answers

All 7 Replies

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
End If

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.

Try this :

SaveFileDialog1.FileName = Date.Now

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"
        Dim saver As New IO.StreamWriter(SaveFileDialog1.FileName)
    End Sub

Nevermind I fixed it with this


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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.