ASP.NET File IO Issue – System.UnauthorizedAccessException – stranger than you think

asp.netfile-io

I am getting a strange issue where I seem to have read access, because I can
1. Get a list of files from a directory (Directory.GetFiles())
2. Load an XML document using XmlDocument instance's Load() method

But I can't use File.ReadAllText() to load a text file into memory. Gives me an System.UnauthorizedAccessException. I am not even trying to read from a network directory, just a local one. I've also used System.Security.Principal.WindowsIdentity.GetCurrent().Name to check the working user which is [CompanyDomain]/[MyUserName] and this user has full access to the directory I am using. I've also checked that the directory actually exists.

My environment
1. Windows Server 2003 Standard Edition
2. Visual Studio 2008
3. Just using the built in web server that launches every time i run the project.

Note: I couldn't find the IUSR_MACHINENAME user on this machine.

Any idea what steps I should take next?

Cheers,
James

Best Answer

One thing to assert:

  • the file that can be Xml Load()-ed is the very same file that cannot be ReadAlText()-ed ?

When things get odd like this, I found that turning auditing on, at the level of the directory or even of the file, often ends up pointing me towards a proper diagnostic and hence resolving the issue.

Also: In looking online reference for ReadAllText() I noted that (oddly, I think), this exception can be caused by:

    path specified a file that is read-only.

Not sure why write access should be sought by this apparent read-only operation, but, maybe just try to make the file r/w-able.