473,575 Members | 3,346 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
FileNotFoundExc eption ...but there is got to be a better way!

Any ideas?

Thanks in advance.

Jun 15 '07 #1
7 19059
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.co m

"sprash" <sp******@gmail .comwrote in message
news:11******** **************@ c77g2000hse.goo glegroups.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
FileNotFoundExc eption ...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
"FileNotFoundEx ception" 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.goo glegroups.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
FileNotFoundExc eption ...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
FileNotFoundExc eption ...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.GetFi les 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.co mwrote:
If you don't have rights then checking for the length will throw a
"FileNotFoundEx ception" 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.goo glegroups.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
FileNotFoundExc eption ...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.goo glegroups.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.co mwrote:
>If you don't have rights then checking for the length will throw a
"FileNotFoundE xception" 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.go oglegroups.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
FileNotFoundExc eption ...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(iniFil eName);
try
{
if (ini.Length != 0) // This will throw FileNotFoundExc eption if
file does not exist but will report a valid length even if I do not
have good permissions
{
if (File.Exists(in iFileName) == false) //This will return
false if I do not have necessary permissions.
{
ErrorMsg = "The configuration file " + iniFileName + "
could not be accessed.";
Trace.Error(Res ult.ErrorMsg);
Result.Success = false;
return Result;
}
}
}
catch (FileNotFoundEx ception 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.co mwrote:
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.co mwrote:
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.goo glegroups.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.co mwrote:
If you don't have rights then checking for the length will throw a
"FileNotFoundEx ception" 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.go oglegroups.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
FileNotFoundExc eption ...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.c omwrote in
message news:Oe******** ******@TK2MSFTN GP02.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.co m

"sprash" <sp******@gmail .comwrote in message
news:11******** **************@ c77g2000hse.goo glegroups.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
FileNotFoundEx ception ...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
4684
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 server in the domain. An asp page in the web attempts to open (or any other access) the remote file and fails. Auditing is enabled on the...
13
2296
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( StrPathToScript ) never gets called! why? If File.Exists( StrPathToScript ) Then MessageBox.Show( StrPathToScript )
3
4940
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 occurred, I don't particularly care which call failed - just that one of them did. I was thinking I might simply check the return value of the last call...
0
2852
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 work. In the code snippet I've included below, it always returns "true" whether the folder/file exists or not (let alone whether or not they have...
18
3156
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 Documents". When I move it to a directory directly under the root, File.Exists returns TRUE. So I tried using FileIOPermission to give me rights to...
2
14391
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 states, "If the caller does not have sufficient permissions to read the specified file, no exception is thrown and the method returns false regardless...
6
13707
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 error (seems to be happening when I try creating a directory) If dirmgr.Exists("s:\blah\" & txt_name.Text) Then lblerror.Text = lblerror.Text &...
7
2301
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 Where "O:\DTX Import UMAR\AsTester.txt" is a network path.
26
4931
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 *in, *out;
0
7845
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8120
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8281
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7868
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5664
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5338
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3778
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3797
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2286
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.