[英]Access to the path (path name) is denied using File.Open
在Windows 10计算机VS2015上进行本地调试。
我需要以“只读”权限打开文件。 该文件存在于Web应用程序的子文件夹中。
这将引发异常:
System.UnauthorizedAccessException:拒绝访问路径“ F:\\ webroot \\ subfolder”。
我尝试添加具有完全权限的每个人,添加具有完全权限的每个用户,打开安全策略审核并检查安全日志以发现发出请求的用户,但其中什么都没有出现,就好像这实际上不是安全错误,而是其他文件夹名称,以及我可以在线找到的所有其他内容,包括添加ASPNET用户-没有要添加的此类用户。
计算出的路径是正确的物理磁盘路径。 路径在webroot内部。
string FilePath = HttpContext.Current.Server.MapPath("\\Upload");
string PathToFile = FilePath + "\\" + idFileName;
Stream fs = File.Open(FilePath, FileMode.Open, FileAccess.Read);
代码的最后一行抛出异常。
ASP.NET网页输出:拒绝访问路径'F:\\ webrootname \\ Upload'。
应用程序事件日志:
事件代码:4011事件消息:发生未处理的访问异常。
可能是因为您要使用FilePath
而不是您计算出的PathToFile
调用FileOpen
,所以:
Stream fs = File.Open(PathToFile, FileMode.Open, FileAccess.Read);
另外,您可以在打开文件之前进一步检查文件是否存在:
string FilePath = HttpContext.Current.Server.MapPath("\\Upload");
string PathToFile = FilePath + "\\" + idFileName;
if(System.IO.File.Exists(PathToFile))
{
Stream fs = File.Open(PathToFile, FileMode.Open, FileAccess.Read);
}
else
{
// use whatever logger to trace your application
log.Error("The file : + PathToFile + " does not exist");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.