DhavalR
DhavalR

Reputation: 1417

C# results FALSE at `File.Exists` even if the file EXISTS

As title says I don't know what's wrong with my code but if (File.Exists) give negative result even if the file is there.

Below is my code

if (File.Exists(ZFileConfig.FileName.Replace(".xml", "_abc.xml")))

Here, ZFileConfig.FileName is E:\\Application\\Application\\bin\\Debug\\resources\\FirstFile.xml

And amazingly ZFileConfig.FileName.Replace(".xml", "_abc.xml") gives me E:\\Application\\Application\\bin\\Debug\\resources\\FirstFile_abc.xml that is what is needed. EVENTHOUGH IF falied to return TRUE.

enter image description here

enter image description here

Upvotes: 5

Views: 4392

Answers (2)

Ben Voigt
Ben Voigt

Reputation: 283624

It looks like your file is actually named abc_RotateFlip.xml.xml.

I can't imagine why any programmer would ever allow hidden file extensions, but your Excel file shows that they are indeed hidden. Turn that off! Choose to know what's going on inside your computer!

enter image description here

You can also use this registry script to change that setting;

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced]
"HideFileExt"=dword:00000000

Upvotes: 9

user1968030
user1968030

Reputation:

Please check with FileInfo :

FileInfo fi = new FileInfo(@"_abc.xml");
bool isExists = fi.Exists;

Generally if you are performing a single operation on a file, use the File class. If you are performing multiple operations on the same file, use FileInfo.

The reason to do it this way is because of the security checking done when accessing a file. When you create an instance of FileInfo, the check is only performed once. However, each time you use a static File method the check is performed.

Upvotes: 3

Related Questions