Checking if a file exists regardless of the permissions on it

S

sprash

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.
 
A

Andy Bates

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
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
B

Barfy the Wonder Camel

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.
 
S

sprash

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".
 
A

Andy Bates

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
 
S

sprash

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;
}


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
 
B

Ben Voigt [C++ MVP]

Nicholas Paldino said:
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]
- (e-mail address removed)

sprash said:
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.
 

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