System.UnauthorizedAccessException loading file from Disk

 ̄綄美尐妖づ 提交于 2019-12-11 05:26:55

问题


Really weird problem loading a file from disk:

string path = HttpContext.Current.Server.MapPath("~/Datasets/blob.xml");
FileStream stream = new FileStream(path, FileMode.Open);

Throws exception:

An exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll but was not handled in user code

Additional information: Access to the path 'D:\webroot\afob\Dev\v1.0.x\AFOB\Datasets\blob.xml' is denied.

The strange thing is it was working 5 minutes ago. I checked permissions on the disk and both the Debugger and ASPNET have read/write rights as do I.

Ideas?


回答1:


Did you dispose the stream the last time you opened it:

string path = HttpContext.Current.Server.MapPath("~/Datasets/blob.xml");
using (var stream = new FileStream(path, FileMode.Open))
{
    ...
}

But in this case I suspect it's really a permissions issue. You could procmon from SysInternals to see exactly what process is trying to open the file and under which account this process executes.



来源:https://stackoverflow.com/questions/5330402/system-unauthorizedaccessexception-loading-file-from-disk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!