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.