dermdaly dermdaly - 5 months ago 34
ASP.NET (C#) Question

Getting Access denied trying to read a file in App_Data in a ASP.NET project - Any pointers?

I've an XML file stored in App_Data which is only used to read some configuration on startup. When I attempt to open it within my code, I get an Access to the path ... is denied message.

I'm having trouble understanding why, and how to solve it. I guess in theory I should alter the permissions on the file (and directory), but this hasn't changed anything.

To give a bit more background:
1. Its a file bundled with the project, so the setup created by VS2008 writes it to the correct place during install. I didn't think I'd need to do anything unusual post install such that the ASP.NET application would have access.
2. When I've tried publishing this locally, I've not had any problems. - This is only on a test Windows 2003 server.
3. I've ran a simple page to understand the effective user of IIS - It is NT AUTHORITY\NETWORK SERVICE. I tried granting "Full Access" to this user to this file, and still it doesn't work.

The code to open it is simple:

FileStream fs = new FileStream(Server.mapPath(s), FileMode.Open);

Where s is defined as '~/App_Data/myfile.xml

I've even copied and pasted the full path & filename (from the exception) and tried "dir" on it, and it is indeed present (to get around a typo for example).

I've also tried running aspnet_regiis.exe /s as per some suggestions out there.

This has me baffled.


Actually, I think I found it. I needed to open the file as follows:

new FileStream(Server.MapPath(s), FileMode.Open, FileAccess.Read);