Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does File.Exists("c:\filename.txt/") return true?

Tags:

c#

.net

file-io

For the file C:\filename.txt,

File.Exists(@"c:\filename.txt/"); 

returns true. But when we then try to open with that filename, it fails.

The filename is bad because of the trailing /. But how can we test to know if a file is valid as this tells me it is not only ok, but that the file exists.

Update: In a standard command line test app the results are as expected (false). But in my custom uri handler the File.Exists() returns true. It's really bizarre.

like image 275
David Thielen Avatar asked Nov 12 '13 17:11

David Thielen


1 Answers

Usually, when querying a system folder, file visualization is the culprit in this kind of issue. IE the file exists at the virtual store location but not in the actual queried path. As a result, attempting to open it will fail. So, before you state that the file doesn't exist. . . you should make sure it really doesn't exist.

like image 114
iheanyi Avatar answered Oct 23 '22 10:10

iheanyi