File.Exists-错误? [英] File.Exists - Wrong?
问题描述
我正在尝试查看图片是否存在,但这说明那里不存在,并且路径正确! 路径是正确的,并且有图片,但是始终转到"else".
i'm trying to see if the picture exists but this says that isnt there, and the path is correct! The path is correct, and it has a picture, but this allways go to "else".
string path = @"c:\folder\pic.jpg";
if (File.Exists(path))
{
//Do something here
}
else
{
}
推荐答案
可能是权限问题.从文档:
如果调用者没有足够的权限来读取指定的文件,则不会抛出异常,并且无论路径是否存在,该方法都将返回false.
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.
当然,这意味着与客户端应用程序相比,在运行Web应用程序(通常在权限降低的情况下运行)时,您更有可能看到此问题.
Of course, this means you're more likely to see this issue when you're running a web app (which typically runs under reduced permissions) than a client app.
此外,如deerchao的注释中所述,File.Exists
仅在给定的路径是文件而不是目录的情况下才返回true.再次,从文档中:
Additionally, as noted in deerchao's comment, File.Exists
only returns true if the path given is to a file, not a directory. Again, from the documentation:
如果路径描述目录,则此方法返回false.
If path describes a directory, this method returns false.
这篇关于File.Exists-错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!