问题
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