473,761 Members | 9,266 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 19115
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
4697
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 remote system and for the file in question but no access denied records are being recorded.
13
2321
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
4947
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 to fprintf. Is it reasonable to assume that if one call to fprintf fails, then all subsequent...
0
2861
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 permissions). Any ideas? <<< BEGIN CODE SNIPPET >>>
18
3180
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 read it, but still no luck. Any suggestions? Thanks... FileIOPermission f = new...
2
14407
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 of the existence of path."
6
13728
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 & "Unable to build physical path. " &
7
2317
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
4957
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
10115
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9957
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9775
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8780
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7332
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6609
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5373
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3456
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2752
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.