简体   繁体   English

即使文件为EXISTS,C#在`File.Exists`处的结果为FALSE

[英]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. 正如标题所说,我不知道我的代码有什么问题,但是if (File.Exists)即使文件存在也会给出否定结果。

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 这里, ZFileConfig.FileNameE:\\\\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. 令人惊讶的是, ZFileConfig.FileName.Replace(".xml", "_abc.xml")为我提供了E:\\\\Application\\\\Application\\\\bin\\\\Debug\\\\resources\\\\FirstFile_abc.xml EVENTHOUGH IF falied to return TRUE. EVENTHOUGH IF愚弄返回TRUE。

在此输入图像描述

在此输入图像描述

It looks like your file is actually named abc_RotateFlip.xml.xml . 看起来您的文件实际上名为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. 我无法想象为什么任何程序员都会允许隐藏文件扩展名,但是你的Excel文件显示它们确实是隐藏的。 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

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. 通常,如果要对文件执行单个操作,请使用File类。 If you are performing multiple operations on the same file, use FileInfo. 如果要对同一文件执行多个操作,请使用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. 创建FileInfo实例时,仅执行一次检查。 However, each time you use a static File method the check is performed. 但是,每次使用静态File方法时都会执行检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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