C# File.Exists 返回 false,文件确实存在

2024-02-15

使用 VS 15、C# 和 .Net 4.5.2
该计算机位于 AD 网络上,广告名称为“AD”。
AD 普通用户权限、AD 管理员权限和本地管理员权限都会出现此问题。无论程序获得什么权限,都会出现同样的问题。

我们的测试文件是“C:/windows/system32/conhost.exe".
上面的文件存在,它非常存在。我可以用资源管理器看到它。

This is the file in explorer:
enter image description here

This is the file properties:
enter image description here

你可以看到它就在那里,对吗?
以下 cmd 命令检查文件是否存在:

IF EXIST "C:\windows\system32\conhost.exe" (echo does exist) ELSE (echo doesnt exist)

它返回“确实存在“ 按照承诺。

以下 C# 代码检查文件是否存在:

FileInfo file = new FileInfo("C:/windows/system32/conhost.exe");
MessageBox.Show(file.Exists + "");

这将返回“False".

此代码还返回“False":

MessageBox.Show(File.Exists("C:/windows/system32/conhost.exe") + "");

此代码也找不到它:

foreach (string file in Directory.GetFiles("C:/windows/system32/"))
{
    //conhost is NEVER mentioned, like it doesn't exist
}

此代码也找不到它:

foreach (string file in Directory.EnumerateFiles("C:/windows/system32/"))
{
    //conhost is NEVER mentioned, like it doesn't exist
}

假,假,假:

MessageBox.Show(File.Exists("C:/windows/system32/conhost.exe") + "");
MessageBox.Show(File.Exists("C:\\windows\\system32\\conhost.exe") + "");
MessageBox.Show(File.Exists(@"C:\windows\system32\conhost.exe") + "");

我究竟做错了什么?
额外说明:我将 conhost 复制到 C:\conhost.exe,我的程序可以毫无问题地找到它。我的程序还在 system32 中找到其他文件,只是不包括 conhost 和其他一些文件。例如,它发现“connect.dll”位于system32中,因此它不是该目录的读取权限。
更多额外说明:conhost.exe 和 connect.dll 具有相同的安全属性(文件属性中的“安全”选项卡)。


如果你使用的是x64系统,你会有不同的内容c:\Windows\System32x86 和 x64 应用程序的目录。请确保您使用运行批处理文件和 C# 应用程序的相同架构。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

C# File.Exists 返回 false,文件确实存在 的相关文章

随机推荐