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

Checking if a file exists regardless of the permissions on it

P: n/a
Newbie question:

I'm trying to determine if a file physically exists regardless of the
permissions on it

Using File.Exists() returns false if it physically exists but the
process does not have the necessary permissions.
One hack could be to check for length and that would throw a
FileNotFoundException ...but there is got to be a better way!

Any ideas?

Thanks in advance.

Jun 15 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
That's the thing, if you don't have permissions to the file, there
shouldn't be a hack. If you need to know if a file exists, then you either
have to have the permissions changed so you have access to it, or run the
program under an account which has the appropriate permissions.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"sprash" <sp******@gmail.comwrote in message
news:11**********************@c77g2000hse.googlegr oups.com...
Newbie question:

I'm trying to determine if a file physically exists regardless of the
permissions on it

Using File.Exists() returns false if it physically exists but the
process does not have the necessary permissions.
One hack could be to check for length and that would throw a
FileNotFoundException ...but there is got to be a better way!

Any ideas?

Thanks in advance.

Jun 15 '07 #2

P: n/a
If you don't have rights then checking for the length will throw a
"FileNotFoundException" as it won't be able to see the file.

You would need directory browsing rights to know that a file existed;
without them no information will be returned.

Why do you need to check a location to see if a file exists when you don't
have rights to that area? A bit of background information may help as to
what solutions are available.

Regards

- Andy

"sprash" <sp******@gmail.comwrote in message
news:11**********************@c77g2000hse.googlegr oups.com...
Newbie question:

I'm trying to determine if a file physically exists regardless of the
permissions on it

Using File.Exists() returns false if it physically exists but the
process does not have the necessary permissions.
One hack could be to check for length and that would throw a
FileNotFoundException ...but there is got to be a better way!

Any ideas?

Thanks in advance.

Jun 15 '07 #3

P: n/a
On Jun 15, 11:55 am, sprash <spras...@gmail.comwrote:
Newbie question:

I'm trying to determine if a file physically exists regardless of the
permissions on it

Using File.Exists() returns false if it physically exists but the
process does not have the necessary permissions.
One hack could be to check for length and that would throw a
FileNotFoundException ...but there is got to be a better way!

Any ideas?

Thanks in advance.
If you have access to the directory, you should be able to do a
Directory.GetFiles and just look for the name.

Jun 15 '07 #4

P: n/a
Thanks for all your responses.
Why do you need to check a location to see if a file exists when you don't
have rights to that area?
My service is designed to run as a user on a domain and is expected to
read a file off a UNC path. In case of a configuration oversight, I
want to report the correct error message -- either "The file could not
be found" or "File was found, but it does not have appropriate
permissions".

On Jun 15, 12:04 pm, "Andy Bates" <a...@ussdev.comwrote:
If you don't have rights then checking for the length will throw a
"FileNotFoundException" as it won't be able to see the file.

You would need directory browsing rights to know that a file existed;
without them no information will be returned.

Why do you need to check a location to see if a file exists when you don't
have rights to that area? A bit of background information may help as to
what solutions are available.

Regards

- Andy

"sprash" <spras...@gmail.comwrote in message

news:11**********************@c77g2000hse.googlegr oups.com...
Newbie question:
I'm trying to determine if a file physically exists regardless of the
permissions on it
Using File.Exists() returns false if it physically exists but the
process does not have the necessary permissions.
One hack could be to check for length and that would throw a
FileNotFoundException ...but there is got to be a better way!
Any ideas?
Thanks in advance.

Jun 15 '07 #5

P: n/a
The system can't tell you explicitly that you don't have rights to an area
as it would be a potential security risk.

If it did then an application could request information from your system and
keep iterating until it found an area that it could access (could be
accounts, personal information that should be secured but wasn't configured
correctly).

If you know that a file should exist at a specific location then if the
request to File.Exist or File.OpenRead fails (i.e. raises an exception) then
it'll probably be an access rights or sharing issue.

- Andy

"sprash" <sp******@gmail.comwrote in message
news:11**********************@z28g2000prd.googlegr oups.com...
Thanks for all your responses.
>Why do you need to check a location to see if a file exists when you
don't
have rights to that area?

My service is designed to run as a user on a domain and is expected to
read a file off a UNC path. In case of a configuration oversight, I
want to report the correct error message -- either "The file could not
be found" or "File was found, but it does not have appropriate
permissions".

On Jun 15, 12:04 pm, "Andy Bates" <a...@ussdev.comwrote:
>If you don't have rights then checking for the length will throw a
"FileNotFoundException" as it won't be able to see the file.

You would need directory browsing rights to know that a file existed;
without them no information will be returned.

Why do you need to check a location to see if a file exists when you
don't
have rights to that area? A bit of background information may help as to
what solutions are available.

Regards

- Andy

"sprash" <spras...@gmail.comwrote in message

news:11**********************@c77g2000hse.googleg roups.com...
Newbie question:
I'm trying to determine if a file physically exists regardless of the
permissions on it
Using File.Exists() returns false if it physically exists but the
process does not have the necessary permissions.
One hack could be to check for length and that would throw a
FileNotFoundException ...but there is got to be a better way!
Any ideas?
Thanks in advance.


Jun 15 '07 #6

P: n/a
The thing is that I can get around this problem by following
"Exception based programming" ... but I really want to avoid that!

Here is what I can do:
....
....

FileInfo ini = new FileInfo(iniFileName);
try
{
if (ini.Length != 0) // This will throw FileNotFoundException if
file does not exist but will report a valid length even if I do not
have good permissions
{
if (File.Exists(iniFileName) == false) //This will return
false if I do not have necessary permissions.
{
ErrorMsg = "The configuration file " + iniFileName + "
could not be accessed.";
Trace.Error(Result.ErrorMsg);
Result.Success = false;
return Result;
}
}
}
catch (FileNotFoundException ex)
{
Trace.Error( string.Format("DT INI File {0} not found.",
iniFileName), ex);
Result.Success = false;
Result.ErrorMsg = "The configuration file " + iniFileName + "
does not exist.";
return Result;
}
On Jun 15, 2:07 pm, "Andy Bates" <a...@ussdev.comwrote:
The system can't tell you explicitly that you don't have rights to an area
as it would be a potential security risk.

If it did then an application could request information from your system and
keep iterating until it found an area that it could access (could be
accounts, personal information that should be secured but wasn't configured
correctly).

If you know that a file should exist at a specific location then if the
request to File.Exist or File.OpenRead fails (i.e. raises an exception) then
it'll probably be an access rights or sharing issue.

- Andy


On Jun 15, 2:07 pm, "Andy Bates" <a...@ussdev.comwrote:
The system can't tell you explicitly that you don't have rights to an area
as it would be a potential security risk.

If it did then an application could request information from your system and
keep iterating until it found an area that it could access (could be
accounts, personal information that should be secured but wasn't configured
correctly).

If you know that a file should exist at a specific location then if the
request to File.Exist or File.OpenRead fails (i.e. raises an exception) then
it'll probably be an access rights or sharing issue.

- Andy

"sprash" <spras...@gmail.comwrote in message

news:11**********************@z28g2000prd.googlegr oups.com...
Thanks for all your responses.
Why do you need to check a location to see if a file exists when you
don't
have rights to that area?
My service is designed to run as a user on a domain and is expected to
read a file off a UNC path. In case of a configuration oversight, I
want to report the correct error message -- either "The file could not
be found" or "File was found, but it does not have appropriate
permissions".
On Jun 15, 12:04 pm, "Andy Bates" <a...@ussdev.comwrote:
If you don't have rights then checking for the length will throw a
"FileNotFoundException" as it won't be able to see the file.
You would need directory browsing rights to know that a file existed;
without them no information will be returned.
Why do you need to check a location to see if a file exists when you
don't
have rights to that area? A bit of background information may help as to
what solutions are available.
Regards
- Andy
"sprash" <spras...@gmail.comwrote in message
>news:11**********************@c77g2000hse.googleg roups.com...
Newbie question:
I'm trying to determine if a file physically exists regardless of the
permissions on it
Using File.Exists() returns false if it physically exists but the
process does not have the necessary permissions.
One hack could be to check for length and that would throw a
FileNotFoundException ...but there is got to be a better way!
Any ideas?
Thanks in advance.

Jun 15 '07 #7

P: n/a

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:Oe**************@TK2MSFTNGP02.phx.gbl...
That's the thing, if you don't have permissions to the file, there
shouldn't be a hack. If you need to know if a file exists, then you
either have to have the permissions changed so you have access to it, or
run the program under an account which has the appropriate permissions.
You don't need access to a file to see if it exists, you only need
permission for the directory. I think the OP is saying that File.Exists
improperly demands access to the file, while GetLength properly queries the
directory. I guess this needs to be validated and reported as a bug.
File.Exists should succeed when the caller has list permission on the
containing directory, or (read permission on the file and bypass traverse
checking right).
>

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"sprash" <sp******@gmail.comwrote in message
news:11**********************@c77g2000hse.googlegr oups.com...
>Newbie question:

I'm trying to determine if a file physically exists regardless of the
permissions on it

Using File.Exists() returns false if it physically exists but the
process does not have the necessary permissions.
One hack could be to check for length and that would throw a
FileNotFoundException ...but there is got to be a better way!

Any ideas?

Thanks in advance.

Jun 16 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.