Locked FileNotFoundException was unhandled

  • Wednesday, January 18, 2012 4:40 PM
     
      Has Code
     I have a program that can save user's input into a text file and load it back, but whenever I try to open a text 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. Thanks.

All Replies

  • Wednesday, January 18, 2012 9:17 PM
     
     

    First thing: you can erase all the ReadLine and use ReadtoEnd (this will not solve the error, but if the textboxes are multiline so you need to read all the lines);

    Second Thing: you can use a Try...Catch...End Try, this will prevent your app to close or raise an event. Under Catch you'll write what the program should do in case of an error.

    Third Thing: verify that FileName... is a filename :D I mean that if you write an invalid name such as an empty name, a name with strange characters or something else it will crush... The error you posted show this problem: the file name you specified is not valid.

  • Friday, January 20, 2012 6:34 AM
    Moderator
     
     Answered Has Code

    Hi Hearsemobile,

    Welcome to the MSDN forum.

    You get the error because whether you choose the file to open or not, the following code will be run:

      Dim loader As New IO.StreamReader(OpenFileDialog1.FileName)
            TextBox1.Text = loader.ReadLine
            ……
    
    
    When you not give the filename, the loader can’t open the file, and will give you an error. So I suggest you to put all the code in a “if … end if” block, here is a sample:
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Dim openFileDialog1 As New OpenFileDialog()
            openFileDialog1.InitialDirectory = "C:\"
            openFileDialog1.Filter = "Text Files ONLY (*.txt) | *.txt"
            openFileDialog1.ShowDialog()
      
            If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
                Try
                    Dim loader As New IO.StreamReader(openFileDialog1.FileName)
                    TextBox1.Text = loader.ReadLine
                    '......
                    loader.Close()
                Catch Ex As Exception
                    MessageBox.Show("Cannot read file from disk. Original error: " & Ex.Message)
                End Try
            End If
        End Sub
    
    
    The below sample show to your another issue: the initial file name for file saving is today's date. It is showed in the SaveFileDialog:
     
    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
            Dim saveFileDialog1 = New SaveFileDialog()
            saveFileDialog1.CreatePrompt = True
            'saveFileDialog1.FileName = Date.Now 
            saveFileDialog1.FileName = Date.Now.Date
            saveFileDialog1.Filter = "Text Files ONLY (*.txt) | *.txt"
            If saveFileDialog1.ShowDialog() = DialogResult.OK Then
                'add your code
            End If
        End Sub
    
    

    If you have any additional questions, please feel free to let me know.


    Mark Liu-lxf [MSFT]
    MSDN Community Support | Feedback to us
  • Monday, January 30, 2012 9:52 AM
    Moderator
     
     

    Hi Hearsemobile,

    We haven’t heard from you for a while. I’d like to mark my reply as answer firstly. If you have any additional questions, you also can unmark the replay and post your question here. 

    Sorry for any inconvenience and have a nice day.


    Mark Liu-lxf [MSFT]
    MSDN Community Support | Feedback to us