RKM
RKM

Reputation: 3241

File Not Found, but the file is there!

public static List<Product> Load(string filename)
{
    if (!File.Exists(filename))
    {
        throw new FileNotFoundException("Data could not be found ", filename );
    }
}

Visual Studio 2010 gives the following exception, "FileNotFoundException"

emmm.. ok. this problem seem to have been solved.

.

But however, I still can not find the file!! But the file is there, in the same directory, Ive already verified and double-verified the name is correct! I have no idea what is going on.

The file is called "Products.xml".

Upvotes: 2

Views: 17852

Answers (7)

Hans Passant
Hans Passant

Reputation: 942257

The file is called "Products.xml".

You expose yourself to random failure with a filename like that. You should use the full path name of the file, like c:\mumble\foo\products.xml. If you don't then you completely rely on your program's working directory being set correctly. The value of Environment.CurrentDirectory.

Even if it is set correctly by whatever program is starting yours (like a shortcut on the desktop), you still can get into trouble when code you didn't write changes the working directory. A good example is OpenFileDialog with the RestoreDirectory property left to the default value of false.

Always use full path names in your code. Or let the user select the file.

Upvotes: 1

DaveRead
DaveRead

Reputation: 3413

File.Exists may return false if the user that the code runs under does not have access to the file, as well as if the does not exist.

http://msdn.microsoft.com/en-us/library/system.io.file.exists.aspx

Upvotes: 2

Ian
Ian

Reputation: 34539

You either:

A) Don't want to throw the exception via this line

throw new FileNotFoundException()

and instead want to display a Dialog to the user, or use some other error handling technique in there. To output an error either use one of the following:

Console.WriteLine("File not found") 
MessageBox.Show("File not found");

B) Higher up in your call stack have a try/catch and handle your error there, similarly with a dialog or another error handling approach suitable for your application.

try
{
   Load(filename);
}
catch(FileNotFoundException fe)
{}

Upvotes: 0

MUS
MUS

Reputation: 1450

In you code first check for empty filename as the passed parameter may be empty string, plus apply try catch block on the code as the passed filename might not satisfy the path rules for a file. in catch block through your exception too.

Upvotes: 0

JaredPar
JaredPar

Reputation: 755357

It looks like the problem is File.Exists is returning false and you're throwing an exception which is not handled by your code. Did you intend for this exception to be handled or does this represent a fatal error to your program?

Upvotes: 1

Kevin
Kevin

Reputation: 5694

The file located at filename does not exist, and thus it throws the exception with the following line: throw new FileNotFoundException("Data could not be found ", filename );

Did you mean to just output an error?

Upvotes: 0

Daniel A. White
Daniel A. White

Reputation: 191037

You are the one who is throwing the exception. Do you mean to put up an error message?

Upvotes: 2

Related Questions