Nick
Nick

Reputation: 19684

File not found Exception.. But it's there

Hey this is going to be one of those dumb questions. I am trying to pick up a file on my local system and I keep getting a FileNotFoundException thrown.

Someone set me straight please :)

if( File.Exists(@"C:\logs\hw-healthways-prod_2009-08-26.tar"))
{
     Console.WriteLine("Yay");
}
else
{
     throw new FileNotFoundException();   
}

Tried moving the file into the same location as the executing application and did the following:

 if( File.Exists("hw-healthways-prod_2009-08-26.tar"))

Same thing.

Then I made a random txt file and parked it there too.. "me.txt"

And it works?! So you thing the file name is the problem?

Upvotes: 23

Views: 53724

Answers (4)

i31nGo
i31nGo

Reputation: 1502

May be the name of the file is "hw-healthways-prod_2009-08-26.tar.tar" instead of "hw-healthways-prod_2009-08-26.tar", I had this problem because by default the extension files are hidden on windows

Upvotes: 16

user37875
user37875

Reputation: 14224

C:\logs\hw-healthways-prod_2009-08-26.tar should be C:\\logs\\hw-healthways-prod_2009-08-26.tar. \ means the next character is an escape character.

Upvotes: -6

Daniel Pryden
Daniel Pryden

Reputation: 60997

Try doing Directory.GetFiles(@"C:\logs"). It's possible that the file in question has odd characters that are getting interpreted one way by Windows Explorer (presumably where you're reading "the file's property" from?) but a different way by the .NET Framework. This can happen if you have UTF-8 characters in the filename (perhaps an en dash?).

Upvotes: 16

user161433
user161433

Reputation: 4519

You may want to check your file permissions. Your computer may not have permission to the file.

Upvotes: 0

Related Questions