By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,894 Members | 1,937 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,894 IT Pros & Developers. It's quick & easy.

How to programatically test file permissions

P: n/a
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
Sep 27 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On 27/09/2006 in message <OG**************@TK2MSFTNGP05.phx.gblGabe
Moothart wrote:
>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?
There's a very useful UserFileAccessRights class on CodeProject:
http://www.codeproject.com/useritems...cessRights.asp

Might that help?

--
Jeff Gaines
Sep 27 '06 #2

P: n/a
Jeff Gaines wrote:
There's a very useful UserFileAccessRights class on CodeProject:
http://www.codeproject.com/useritems...cessRights.asp

Might that help?
Yep, that's what I was looking for. Thanks!

Gabe
Sep 27 '06 #3

P: n/a
Some remarks inline.

Willy.

"Gabe Moothart" <ga**@imaginesystems.netwrote in message
news:OG**************@TK2MSFTNGP05.phx.gbl...
| 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.
Sep 28 '06 #4

P: n/a

"Gabe Moothart" <ga**@imaginesystems.netwrote in message
news:eo**************@TK2MSFTNGP06.phx.gbl...
| Jeff Gaines wrote:
| There's a very useful UserFileAccessRights class on CodeProject:
| http://www.codeproject.com/useritems...cessRights.asp
| >
| Might that help?
|
| Yep, that's what I was looking for. Thanks!
|
| Gabe

Note that calling these methods are expensive, you should only call them
when a File Open request has thrown.

Willy.

Sep 28 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.