简体   繁体   English

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

[英]C# File.Exists returns false, file does exist

Using VS 15, C# with .Net 4.5.2 使用VS 15,C#和.Net 4.5.2
The computer is on an AD network, with the ad name "AD". 计算机位于AD网络上,广告名称为“AD”。
This problem happens with AD normal-user rights, AD admin rights, and local admin rights. AD正常用户权限,AD管理员权限和本地管理员权限会发生此问题。 It doesn't matter what rights the program gets, the same problem occurs. 无论程序获得什么权利,都会出现同样的问题。

Our test file is " C:/windows/system32/conhost.exe ". 我们的测试文件是“ C:/windows/system32/conhost.exe ”。
The file above exists, it is very much existing. 上面的文件存在,它是非常存在的。 I can see it with explorer. 我可以用资源管理器看到它。

This is the file in explorer: 这是资源管理器中的文件:
在此输入图像描述

This is the file properties: 这是文件属性:
在此输入图像描述

You can see that it is there, right? 你可以看到它在那里,对吗?
The following cmd command checks if the file exists: 以下cmd命令检查文件是否存在:

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

It returns " does exist " as promised. 它按照承诺返回“ 确实存在 ”。

The following C# code checks if the file exists: 以下C#代码检查文件是否存在:

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

This returns " False ". 返回“ False ”。

This code also returns " False ": 此代码也返回“ False ”:

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

This code also doesn't find it: 此代码也找不到它:

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

This code also doesn't find it: 此代码也找不到它:

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

False, False, False: 假,假,假:

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") + "");

What am I doing wrong? 我究竟做错了什么?
Extra note: I copied conhost to C:\\conhost.exe, and my program can find that without problem. 额外注意:我将conhost复制到C:\\ conhost.exe,我的程序可以毫无问题地找到它。 My program also finds other files in system32, just not conhost and a few others. 我的程序还在system32中找到其他文件,只是没有conhost和其他一些文件。 For example, it finds "connect.dll" which is in system32, so it's not the directory's read permission. 例如,它找到system32中的“connect.dll”,因此它不是目录的读取权限。
More extra notes: conhost.exe and connect.dll has the same security attributes (Security tab in the file properties). 更多额外说明:conhost.exe和connect.dll具有相同的安全属性(文件属性中的“安全”选项卡)。

If you are using x64 system, you will have different content of the c:\\Windows\\System32 directory for x86 and x64 applications. 如果您使用的是x64系统,则x86和x64应用程序的c:\\Windows\\System32目录将具有不同的内容。 Please be sure that you are using same architecture running batch file and your C# app. 请确保您使用的是运行批处理文件和C#应用程序的相同架构。

In the MSDN documentation for System.IO.File.Exists(path) , it states: System.IO.File.Exists(path)的MSDN文档中,它指出:

If the caller does not have sufficient permissions to read the specified file, no exception is thrown and the method returns false regardless of the existence of path. 如果调用者没有足够的权限来读取指定的文件,则不会抛出任何异常,并且无论路径是否存在,该方法都返回false。

For this reason, we can safely assume that your application does not have read access to that specific file. 因此,我们可以安全地假设您的应用程序没有对该特定文件的读取权限。 Check the security settings and grant read access if not already done so. 如果尚未执行此操作,请检查安全设置并授予读取权限。

Build your application (in release mode) and run as administrator. 构建应用程序(在发布模式下)并以管理员身份运行。

This is the problem that come over 64-bit operating system... here is a work around, 这是来自64位操作系统的问题......这是一个解决方法,

go to the project's properties > click on build tab > untick Prefer 32-bit 转到项目的属性>单击构建选项卡>取消选择首选32位

after that, it should work correctly over 64-bit os. 之后,它应该在64位操作系统上正常工作。

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

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