简体   繁体   中英

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.

在此输入图像描述

在此输入图像描述

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!

在此输入图像描述

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM