How to programatically test file permissions

G

Gabe Moothart

Hello,
In one of my asp.net applications I test the existence of a file, like so:

File.Exists(Server.MapPath("/path/file.jpg")));

This was failing, even though the path returned by Server.MapPath was
clearly present. After some debugging, I discovered that the the asp.net
account didn't have permission to execute File.Exists. Apparently
File.Exists simply returns "false" instead of throwing an exception if
it doesn't have the permissions it needs. This is pretty unintuitive.

Anyway, I would like to add an assert method that ensures I have the
proper permissions so that this does not happen again. How might I go
about doing that?

TIA,
Gabe
 
W

Willy Denoyette [MVP]

Some remarks inline.

Willy.

| Hello,
| In one of my asp.net applications I test the existence of a file, like so:
|
| File.Exists(Server.MapPath("/path/file.jpg")));
|
| This was failing, even though the path returned by Server.MapPath was
| clearly present.

Right, from a human's perspective the file is present, but from a security
perspective she is not. If the callers identity has no rights to read the
file attributes (this is what Exists does), it should return "false", that
is, for you (the caller) the file does not exist.

After some debugging, I discovered that the the asp.net
| account didn't have permission to execute File.Exists.
No, the account can perfectly execute File.Exists method, the caller is
simply missing the privilege required to inspect the file existense.

That's why File.Exists has little or no value, a true value returned doesn't
imply that you can access the file, a false return doesn't mean that the
file isn't present.


Willy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top