C# 为什么 System.IO.File.Exists(string path) 返回 false?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18266637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why does System.IO.File.Exists(string path) return false?
提问by Shilpa Soni
System.IO.File.Exists(string path)
returns always false, even when the file exists on the specified path. What could be the possible solution?
始终返回 false,即使文件存在于指定路径上。可能的解决方案是什么?
采纳答案by Jon Skeet
It could well be a permission problem. From the documentation:
这很可能是权限问题。从文档:
The Exists method returns false if any error occurs while trying to determine if the specified file exists. This can occur in situations that raise exceptions such as passing a file name with invalid characters or too many characters, a failing or missing disk, or if the caller does not have permission to read the file.
如果在尝试确定指定文件是否存在时发生任何错误,Exists 方法将返回 false。这可能发生在引发异常的情况下,例如传递包含无效字符或过多字符的文件名、磁盘出现故障或丢失,或者调用方没有读取文件的权限。
One way of seeing what's happening is to just try to read the file (e.g. with File.OpenRead
). I'd be surprised if that succeeds- but if it fails, the exception should give you more information.
查看正在发生的事情的一种方法是尝试读取文件(例如使用File.OpenRead
)。如果成功,我会感到惊讶- 但如果失败,异常应该为您提供更多信息。
回答by F U
How I got around this was using Server.MapPath(fileName)
as it kept trying to find the file somewhere else.
我是如何解决这个问题的,Server.MapPath(fileName)
因为它一直试图在其他地方找到文件。
System.IO.File.Exists(Server.MapPath(string path))
回答by Krondorian
I was experiencing this myself too. In my case, I was deleting the file and re-creating it. In the process that was deleting the file, I forgot to add in WaitForExit()
before using File.Exists
later on
我自己也经历过这种情况。就我而言,我正在删除该文件并重新创建它。在被删除文件的过程中,我忘了添加WaitForExit()
使用前File.Exists
后来
回答by bugmagnet
Hiding file endings in windows can sometimes cause confusion: you KNOW your file is named file.txt when it is actually named file.txt.txt because the last 4 characters have been hidden by the OS.
在 Windows 中隐藏文件结尾有时会引起混淆:当文件实际命名为 file.txt.txt 时,您知道文件名为 file.txt,因为操作系统隐藏了最后 4 个字符。
回答by Rm558
in my case, a different "dash" in file name causes the issue.
就我而言,文件名中的不同“破折号”会导致问题。
var f1 = "4-37R.pdf";
var f2 = "4‐37R.pdf";
var r = f1==f2?"same":"diff";
Console.Write(r); //diff
turns out
结果
var c1 = '-';
var c2 = '‐';
Console.WriteLine((int)c1); //45
Console.WriteLine((int)c2); //8208
use the same '-' fixes the issue.
使用相同的“-”解决问题。
回答by Ionselat
This had me stumped for a while while I was debugging a service locally, I was running File.Exists("U:\dir1") against a server location mapped on my workstation as (U:). I replaced the U:\dir1 to "\\serverPath\dir1" and File.Exists then returned true.
当我在本地调试服务时,这让我难住了一段时间,我正在对映射在我的工作站上的服务器位置运行 File.Exists("U:\dir1") (U:)。我将 U:\dir1 替换为“\\serverPath\dir1”,然后 File.Exists 返回 true。
回答by NGambit
One possibility not mentioned in any of the answers here is 'File System Redirection'on Windows 8.1 onward.
这里的任何答案中都没有提到的一种可能性是Windows 8.1 及更高版本上的“文件系统重定向”。
For example, if your program is a 32-bit application and you're running on 64-bit Windows then an attempt to access %windir%\System32 would be redirected to %windir%\SysWOW64. And if the file you're trying to access doesn't exist in %windir%\SysWOW64 then System.IO.File.Exists(string path) would return False.
例如,如果您的程序是 32 位应用程序并且您在 64 位 Windows 上运行,那么访问 %windir%\System32 的尝试将被重定向到 %windir%\SysWOW64。如果您尝试访问的文件在 %windir%\SysWOW64 中不存在,那么 System.IO.File.Exists(string path) 将返回 False。
回答by Tammar
System.IO.File.Exists(string path) returned false for me while trying to read C:\OpenSSL\bin\file.txt
.
Running the application in Administrator mode did not help.
(I was logged on the administrator account, Windows 10)
Once I moved the file to C:\Users\MyUser\Desktop\file.txt
, File.Exists() returned true.
System.IO.File.Exists(string path) 在尝试读取C:\OpenSSL\bin\file.txt
. 在管理员模式下运行应用程序没有帮助。(我登录的是管理员帐户,Windows 10)将文件移动到 后C:\Users\MyUser\Desktop\file.txt
,File.Exists() 返回 true。
回答by D. Haskins
I was puzzling over this as well, then realized I was using File.Exists when I should have been using Directory.Exists.
我也对此感到困惑,然后意识到我在使用 File.Exists 时我应该使用 Directory.Exists。
回答by Rob
I just learned today that System.IO.File.Exists will return false if the file exists but is empty
我今天刚刚了解到,如果文件存在但为空,System.IO.File.Exists 将返回 false