为什么 System.IO.File.Exists(string path) 返回 false? [英] Why does System.IO.File.Exists(string path) return false?
问题描述
System.IO.File.Exists(string path)
始终返回 false,即使文件存在于指定路径上.可能的解决方案是什么?
returns always false, even when the file exists on the specified path. What could be the possible solution?
推荐答案
这很可能是权限问题.来自文档:
It could well be a permission problem. From the documentation:
如果在尝试确定指定文件是否存在时发生任何错误,Exists 方法将返回 false.这可能发生在引发异常的情况下,例如传递包含无效字符或过多字符的文件名、磁盘出现故障或丢失,或者调用方没有读取文件的权限.
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.
查看正在发生的事情的一种方法是尝试读取文件(例如使用 File.OpenRead
).如果 成功,我会感到惊讶 - 但如果失败,异常应该为您提供更多信息.
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.
这篇关于为什么 System.IO.File.Exists(string path) 返回 false?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!