views:

608

answers:

3

Hi,

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

A: 

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.

mjv
I think the 'path specified a file that is read-only' portion from the msdn help is just the result of a documentation writer copying-and-pasting stuff... ...the ReadAllText method opens a StreamReader on the file, which in turn opens a FileStream in read-mode, with sharing-mode read.
KristoferA - Huagati.com
@KristoferA, thank you for checking on this. I thought it might have been the case [of a oversight from the MSFT documentation editor], but it is good to have this confirmed. So I guess the OP is then left with my initial suggestion of seeing what the file audit could reveal, and taking it from there...
mjv
Thanks for trying to help, was a copy and paste error it turned out, tried to read directory instead of file
James Z.
A: 

Is anyone else reading or writing the same file at the same time? Perhaps having it open with 'no sharing' file sharing mode..?

KristoferA - Huagati.com
no one else is reading or writing the same file, just reading in some sql templates for code gen.
James Z.
A: 

http://msdn.microsoft.com/en-us/library/72wdk8cc%28VS.71%29.aspx

<identity impersonate="true" />
Noon Silk
Please, do explain a bit more... I'm seeing the <identity> .NET config parameter as yet another place where one can define the account "at the wheel" when the web application logic runs, but I'm not sure this param or lack thereof explains the problem described, whereby XML Load() works, but not ReadAllText().
mjv
I'd just try it. I'm generally suspicious of you being able to do one and not the other; I'd like to see you reproduce that exactly, in a smaller application outside of your main one.
Noon Silk
i've tried this already, it doesn't work.
James Z.
Can you work up a small example in a new project and see if you can reproduce it?
Noon Silk
And you are right to be suspicious, I haven't come back to this for a while now cause I've used hard coding instead of loading templates from the directory. I've just re-read the code again and found that I was trying to ReadAllText() the directory instead of the file.
James Z.
Hilarious. Glad it's solved :)
Noon Silk