Kri*_*sen 28 c# file file-exists
使用VS 15,C#和.Net 4.5.2
计算机位于AD网络上,广告名称为"AD".
AD正常用户权限,AD管理员权限和本地管理员权限会发生此问题.无论程序获得什么权利,都会出现同样的问题.
我们的测试文件是" C:/windows/system32/conhost.exe ".
上面的文件存在,它是非常存在的.我可以用资源管理器看到它.
你可以看到它在那里,对吗?
以下cmd命令检查文件是否存在:
IF EXIST "C:\windows\system32\conhost.exe" (echo does exist) ELSE (echo doesnt exist)
Run Code Online (Sandbox Code Playgroud)
它按照承诺返回" 确实存在 ".
以下C#代码检查文件是否存在:
FileInfo file = new FileInfo("C:/windows/system32/conhost.exe");
MessageBox.Show(file.Exists + "");
Run Code Online (Sandbox Code Playgroud)
返回" False ".
此代码也返回" False ":
MessageBox.Show(File.Exists("C:/windows/system32/conhost.exe") + "");
Run Code Online (Sandbox Code Playgroud)
此代码也找不到它:
foreach (string file in Directory.GetFiles("C:/windows/system32/"))
{
//conhost is NEVER mentioned, like it doesn't exist
}
Run Code Online (Sandbox Code Playgroud)
此代码也找不到它:
foreach (string file in Directory.EnumerateFiles("C:/windows/system32/"))
{
//conhost is NEVER mentioned, like it doesn't exist
}
Run Code Online (Sandbox Code Playgroud)
假,假,假:
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") + "");
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
额外注意:我将conhost复制到C:\ conhost.exe,我的程序可以毫无问题地找到它.我的程序还在system32中找到了其他文件,只是没有conhost和其他一些文件.例如,它找到system32中的"connect.dll",因此它不是目录的读取权限.
更多额外说明:conhost.exe和connect.dll具有相同的安全属性(文件属性中的"安全"选项卡).
Nat*_*rad 10
在System.IO.File.Exists(path)的MSDN文档中,它指出:
如果调用者没有足够的权限来读取指定的文件,则不会抛出异常,并且无论路径是否存在,该方法都返回false.
因此,我们可以安全地假设您的应用程序没有对该特定文件的读取权限.如果尚未执行此操作,请检查安全设置并授予读取权限.
构建应用程序(在发布模式下)并以管理员身份运行.