Wednesday, September 12, 2007

Access Denied on a Directory

I don't know why that is, but when trying to access a directory instead of a file with a StreamReader (or anything else that uses it), you will get an Access Denied error...

Try to run the following code:

StreamReader sr = new StreamReader(@"c:\");
Actually - before you run it - try to guess what error it might generate...
Ok - now, make a note of your answer, and run to code.

Well? Was your guess right?

I know, I know - it doesn't seem to make much sense, but the actual error message is:
Access Denied
Apparently, you are not allowed to read from a directory. But the error message is at least misleading. It mislead me for some time when I started looking for the cause of this error.

Of course the code I was debugging was not that simple as the one above - and that's why it took me quite a long time to notice, that I'm pointing to a directory, while wanting to load a file into an XmlDocument.

Anyway - access denied does not always stem from insufficient user rights ;)

No comments: