Answered by:
File Not Found Error

Question
-
Hi ,
Im a complete newbie to vb 2005 express
Im creating a lil program that when i click a button it will read a text file on the hard drive and stream it to a text box
I keep getting FileNotFoundExceptionWasNotHandled
Trouble shooting tips
verify that the file exists in the specified location
when using relative paths make sure the current directory is correct
im trying to avoid using a openfiledialog control
Heres my code
Private
Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim mystream As System.IO.StreamReadermystream = System.IO.File.OpenText(
"C:\A\A.txt") Dim mystring As Stringmystring = mystream.ReadToEnd
mystream.Close()
TextBox1.Text = mystring
End SubAny Help would sure be appreciated Thanks
Saturday, April 19, 2008 3:23 AM
Answers
-
It's pretty simple, just like what the error message said. The file you are trying to access does not exist, therefore an error is thrown. Are you sure the file exists as you have entered the path in the OpenText() method?
Best practice is to make sure you check for the file if it exists before performing an operation. Example:
if System.IO.File.Exists("path\file.txt") then
'code to read file
else
MessageBox.Show("The file path entered does not exist")
end if
be sure that the path you are giving to the file, exists, that it is the correct path. As always, there are more than one way to write the same code. Just for your information and curiosity, here is a shorter way:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
using mystream As System.IO.StreamReader = System.IO.File.OpenText("C:\A\A.txt")
Me.textBox1.Text = mystream.ReadToEnd()
mystream.Close()
end using
the "using" statement is to dispose of the object correctly, so that it frees system resources and releases the memory back to the OS and makes your application more memory friendly, and is best practice to do this.
Hope this helps in some way.
Please DO keep asking questions, we will help and its the only way to learn as you are :-)
Saturday, April 19, 2008 3:53 AM
All replies
-
It's pretty simple, just like what the error message said. The file you are trying to access does not exist, therefore an error is thrown. Are you sure the file exists as you have entered the path in the OpenText() method?
Best practice is to make sure you check for the file if it exists before performing an operation. Example:
if System.IO.File.Exists("path\file.txt") then
'code to read file
else
MessageBox.Show("The file path entered does not exist")
end if
be sure that the path you are giving to the file, exists, that it is the correct path. As always, there are more than one way to write the same code. Just for your information and curiosity, here is a shorter way:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
using mystream As System.IO.StreamReader = System.IO.File.OpenText("C:\A\A.txt")
Me.textBox1.Text = mystream.ReadToEnd()
mystream.Close()
end using
the "using" statement is to dispose of the object correctly, so that it frees system resources and releases the memory back to the OS and makes your application more memory friendly, and is best practice to do this.
Hope this helps in some way.
Please DO keep asking questions, we will help and its the only way to learn as you are :-)
Saturday, April 19, 2008 3:53 AM -
Thank You ahmedilyas.
I'm still having the same problem (same error) I changed the code as you suggested ,and I know the path is correct.
I even went so far as to use the openfiledialog control (to which I wish to avoid) and i can manually point to the file, and stream to my text box. My code now looks like this.
Imports
System.IOPublic
Class Form1mystream.Close()
End Using ElseMessageBox.Show(
"The file path entered does not exist") End If End SubEnd
ClassCould this have to do with current directory and relative paths? Thanks for any help .
Saturday, April 19, 2008 2:56 PM -
Very odd. I see no problem at all, and naturally tested the code and works fine here.
if you are giving a path to the file as you are, then i see no reason why it should say it hasnt found the file. Are you sure that is the EXACT code you are using and not just editing the filepath when you are posting here on the forums?
Saturday, April 19, 2008 3:13 PM -
Hi,
I'm not sure what was going wrong. I recreated the file. And everything works great. Thanks for your help.
Saturday, April 19, 2008 6:36 PM -
great :-)
glad you got it working
Saturday, April 19, 2008 6:40 PM