473,399 Members | 2,858 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Checking if a file exists regardless of the permissions on it

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
7 18993
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
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
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
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
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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Rene' Nielsen | last post by:
Context: Running Windows 2003 Server on an intranet. A web is configured with an anonymous access account that is a domain account that has been granted the desired access to a file on another...
13
by: Joseph Oget | last post by:
If I use the code below in a VB.NET form, even if the file exists, and is then executed, the Else statement produces the "Sorry cannot find the File " error message. The MessageBox.Show(...
3
by: Jim Hunter | last post by:
From what I've read, the only indication fprintf gives of an error is a negative return value. I have a series of writes to a file using fprintf, and, while I need to know if a failure has...
0
by: Jeff | last post by:
I am trying to write some C# code that will allow me to determine if the current user running my application has "write" permissions to a particular folder. However, I can't seem to get it to...
18
by: Dan | last post by:
I have code like the following to test for existence of a file. I know the file is there, but File.Exists returns FALSE. The problem appears to be that the file is in a directory beneath "My...
2
by: Chris Fink | last post by:
I am using the System.IO.File class to determine if a file exists on a network share. The File.Exists method keeps returning false, even though the file does exist. The MSDN documentation...
6
by: Chad Crowder | last post by:
Getting the following error on my production server whether the file exists or not: "System.IO.IOException: Cannot create a file when that file already exists." Here's the code generating the...
7
by: George Lake | last post by:
Now this is strange. I had an app runing on W2K Pro and now that its on XP I cant find a file. The code that is not working is: If File.Exists("O:\DTX Import UMAR\AsTester.txt") Then ...
26
by: Army1987 | last post by:
Is this a good way to check wheter a file already exists? #include <stdio.h> #include <stdlib.h> int ask(const char *prompt); typedef char filename; int main(int argc, char *argv) { FILE...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.