.NETGURU
File.Exists question
Messages   Related Types
This message was discovered on microsoft.public.dotnet.languages.csharp.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.
Post a new message to this list...

Dan
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
FileIOPermission(FileIOPermissionAccess.AllAccess, fileName);
if (!File.Exists(fileName))
throw new FileNotFoundException("Unable to construct
BinaryUploadFile object. File does not exist:\n\n" + fileName + "\n\n");

Reply to this message...
 
    
Nicholas Paldino [.NET/C# MVP] (VIP)
Dan,

If you don't have access to the directory, then Exists will return
false, as you expected.

You are creating the permission to see if you can access the directory,
to prevent a false negative. However, you aren't doing anything to indicate
whether or not you have permission. Between the call to Exists, and the
construction of the permission, call Demand on the permission. It will
throw a SecurityException if you don't have access to the directory.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- Click here to reveal e-mail address

"Dan" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
    
Dan
Thanks Nick, but it didn't seem to work. Changed the code to this (see
below), but Exists still returned false. Any more ideas?

FileIOPermission f = new
FileIOPermission(FileIOPermissionAccess.AllAccess, fileName);
f.Demand();
if (!File.Exists(fileName))
throw new FileNotFoundException("Unable to construct
BinaryUploadFile object. File does not exist:\n\n" + fileName + "\n\n");

"Nicholas Paldino [.NET/C# MVP]" <Click here to reveal e-mail address> wrote in
message news:%23s$Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
    
Nicholas Paldino [.NET/C# MVP] (VIP)
Dan,

Just a question, what is the value being passed in for fileName, and the
actual path to the filename you are trying to find?

--
- Nicholas Paldino [.NET/C# MVP]
- Click here to reveal e-mail address

"Dan" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
    
Nicholas Paldino [.NET/C# MVP] (VIP)
Dan,

It could be that you simply don't have the NTFS permissions (which the
permissions class is not going to pick up), and File.Exists is going to
return false. The Exists method actuall uses the FindFirstFile API function
to determine if the file exists, and I imagine that if the user account the
app is running under doesn't have rights to that directory, then it will
return false to see if it exists.

--
- Nicholas Paldino [.NET/C# MVP]
- Click here to reveal e-mail address

"Dan" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
    
Willy Denoyette [MVP] (VIP)
If you (the windows identity running the code) don't have the right
permissions to the folder holding the file, File.Exists will return false,
as you are experiencing.
This is called "windows access security " based on the identity of the
caller.
You are confusing this security mechanism with .NET's "Code access security"
, this kind of security mechanism is based on the "code identity" (where did
the code came from - internet, intranet, codegroup , ...) NOT the windows
user identity, these are fundamentally different. Whatever you do in code
(that is Code Access Security) won't help if the "windows user" has no
access rights for the objects (like files) controlled by the OS security
system (Windows security).

In short you need to fix the NTFS security settings on the file/folder.

Willy.

"Dan" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
    
Dan
But I'm running with administrative rights, with full access to the files in
that directory. Could it have something to do with running from the
debugger?

"Willy Denoyette [MVP]" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
    
Dennis Myrén
Are you completely sure the file name is correct?

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Dan" <Click here to reveal e-mail address> wrote in message
news:%23qNps$Click here to reveal e-mail address...
> But I'm running with administrative rights, with full access to the files
in
[Original message clipped]

Reply to this message...
 
    
Willy Denoyette [MVP] (VIP)
Ok, make sure the file path is correct and/or check if the administrators
group has access to the folder.

Willy.

"Dan" <Click here to reveal e-mail address> wrote in message
news:%23qNps$Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
    
Dan
Sorry to waste your time. I was pulling the filename out some XML which was
generated on another machine; thus the "My Documents" paths were different.
While I'm at it, is an assembly directive like the one below the best way to
ensure you have file access rights on an assembly-wide basis:

[assembly:PermissionSetAttribute(SecurityAction.RequestMinimum, Name =
"FullTrust")]

"Willy Denoyette [MVP]" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
 
System.IO.File
System.IO.FileNotFoundException
System.Security.Permissions.FileIOPermission
System.Security.Permissions.FileIOPermissionAccess
System.Security.Permissions.PermissionSetAttribute
System.Security.Permissions.SecurityAction
System.Security.SecurityException




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification