Connecting Tech Pros Worldwide Forums | Help | Site Map

File.Exists question

Dan
Guest
 
Posts: n/a
#1: Nov 16 '05
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");



Nicholas Paldino [.NET/C# MVP]
Guest
 
Posts: n/a
#2: Nov 16 '05

re: File.Exists question


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]
- mvp@spam.guard.caspershouse.com

"Dan" <dan@dontspamme.com> wrote in message
news:OsLAPhzlEHA.3452@TK2MSFTNGP15.phx.gbl...[color=blue]
>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");
>
>[/color]


Nicholas Paldino [.NET/C# MVP]
Guest
 
Posts: n/a
#3: Nov 16 '05

re: File.Exists question


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]
- mvp@spam.guard.caspershouse.com

"Dan" <dan@dontspamme.com> wrote in message
news:OsLAPhzlEHA.3452@TK2MSFTNGP15.phx.gbl...[color=blue]
>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");
>
>[/color]


Willy Denoyette [MVP]
Guest
 
Posts: n/a
#4: Nov 16 '05

re: File.Exists question


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" <dan@dontspamme.com> wrote in message
news:OsLAPhzlEHA.3452@TK2MSFTNGP15.phx.gbl...[color=blue]
>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");
>
>[/color]


Willy Denoyette [MVP]
Guest
 
Posts: n/a
#5: Nov 16 '05

re: File.Exists question


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" <dan@dontspamme.com> wrote in message
news:OsLAPhzlEHA.3452@TK2MSFTNGP15.phx.gbl...[color=blue]
>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");
>
>[/color]


Dan
Guest
 
Posts: n/a
#6: Nov 16 '05

re: File.Exists question


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]" <mvp@spam.guard.caspershouse.com> wrote in
message news:%23s$fXozlEHA.2884@TK2MSFTNGP09.phx.gbl...[color=blue]
> 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[/color]
directory,[color=blue]
> to prevent a false negative. However, you aren't doing anything to[/color]
indicate[color=blue]
> 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]
> - mvp@spam.guard.caspershouse.com
>
> "Dan" <dan@dontspamme.com> wrote in message
> news:OsLAPhzlEHA.3452@TK2MSFTNGP15.phx.gbl...[color=green]
> >I have code like the following to test for existence of a file. I know[/color][/color]
the[color=blue][color=green]
> > 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[/color][/color]
to[color=blue][color=green]
> > 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");
> >
> >[/color]
>
>[/color]


Dan
Guest
 
Posts: n/a
#7: Nov 16 '05

re: File.Exists question


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]" <mvp@spam.guard.caspershouse.com> wrote in
message news:%23s$fXozlEHA.2884@TK2MSFTNGP09.phx.gbl...[color=blue]
> 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[/color]
directory,[color=blue]
> to prevent a false negative. However, you aren't doing anything to[/color]
indicate[color=blue]
> 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]
> - mvp@spam.guard.caspershouse.com
>
> "Dan" <dan@dontspamme.com> wrote in message
> news:OsLAPhzlEHA.3452@TK2MSFTNGP15.phx.gbl...[color=green]
> >I have code like the following to test for existence of a file. I know[/color][/color]
the[color=blue][color=green]
> > 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[/color][/color]
to[color=blue][color=green]
> > 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");
> >
> >[/color]
>
>[/color]


Dan
Guest
 
Posts: n/a
#8: Nov 16 '05

re: File.Exists question


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]" <willy.denoyette@pandora.be> wrote in message
news:OWroB6zlEHA.2864@TK2MSFTNGP14.phx.gbl...[color=blue]
> 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[/color]
security"[color=blue]
> , this kind of security mechanism is based on the "code identity" (where[/color]
did[color=blue]
> 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" <dan@dontspamme.com> wrote in message
> news:OsLAPhzlEHA.3452@TK2MSFTNGP15.phx.gbl...[color=green]
> >I have code like the following to test for existence of a file. I know[/color][/color]
the[color=blue][color=green]
> > 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[/color][/color]
to[color=blue][color=green]
> > 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");
> >
> >[/color]
>
>[/color]


Dan
Guest
 
Posts: n/a
#9: Nov 16 '05

re: File.Exists question


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]" <willy.denoyette@pandora.be> wrote in message
news:OWroB6zlEHA.2864@TK2MSFTNGP14.phx.gbl...[color=blue]
> 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[/color]
security"[color=blue]
> , this kind of security mechanism is based on the "code identity" (where[/color]
did[color=blue]
> 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" <dan@dontspamme.com> wrote in message
> news:OsLAPhzlEHA.3452@TK2MSFTNGP15.phx.gbl...[color=green]
> >I have code like the following to test for existence of a file. I know[/color][/color]
the[color=blue][color=green]
> > 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[/color][/color]
to[color=blue][color=green]
> > 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");
> >
> >[/color]
>
>[/color]


Nicholas Paldino [.NET/C# MVP]
Guest
 
Posts: n/a
#10: Nov 16 '05

re: File.Exists question


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]
- mvp@spam.guard.caspershouse.com

"Dan" <dan@dontspamme.com> wrote in message
news:uhSo08zlEHA.1652@TK2MSFTNGP09.phx.gbl...[color=blue]
> 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]" <mvp@spam.guard.caspershouse.com> wrote
> in
> message news:%23s$fXozlEHA.2884@TK2MSFTNGP09.phx.gbl...[color=green]
>> 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[/color]
> directory,[color=green]
>> to prevent a false negative. However, you aren't doing anything to[/color]
> indicate[color=green]
>> 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]
>> - mvp@spam.guard.caspershouse.com
>>
>> "Dan" <dan@dontspamme.com> wrote in message
>> news:OsLAPhzlEHA.3452@TK2MSFTNGP15.phx.gbl...[color=darkred]
>> >I have code like the following to test for existence of a file. I know[/color][/color]
> the[color=green][color=darkred]
>> > 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[/color][/color]
> to[color=green][color=darkred]
>> > 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");
>> >
>> >[/color]
>>
>>[/color]
>
>[/color]


Nicholas Paldino [.NET/C# MVP]
Guest
 
Posts: n/a
#11: Nov 16 '05

re: File.Exists question


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]
- mvp@spam.guard.caspershouse.com

"Dan" <dan@dontspamme.com> wrote in message
news:uhSo08zlEHA.1652@TK2MSFTNGP09.phx.gbl...[color=blue]
> 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]" <mvp@spam.guard.caspershouse.com> wrote
> in
> message news:%23s$fXozlEHA.2884@TK2MSFTNGP09.phx.gbl...[color=green]
>> 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[/color]
> directory,[color=green]
>> to prevent a false negative. However, you aren't doing anything to[/color]
> indicate[color=green]
>> 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]
>> - mvp@spam.guard.caspershouse.com
>>
>> "Dan" <dan@dontspamme.com> wrote in message
>> news:OsLAPhzlEHA.3452@TK2MSFTNGP15.phx.gbl...[color=darkred]
>> >I have code like the following to test for existence of a file. I know[/color][/color]
> the[color=green][color=darkred]
>> > 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[/color][/color]
> to[color=green][color=darkred]
>> > 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");
>> >
>> >[/color]
>>
>>[/color]
>
>[/color]


Nicholas Paldino [.NET/C# MVP]
Guest
 
Posts: n/a
#12: Nov 16 '05

re: File.Exists question


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]
- mvp@spam.guard.caspershouse.com

"Dan" <dan@dontspamme.com> wrote in message
news:uhSo08zlEHA.1652@TK2MSFTNGP09.phx.gbl...[color=blue]
> 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]" <mvp@spam.guard.caspershouse.com> wrote
> in
> message news:%23s$fXozlEHA.2884@TK2MSFTNGP09.phx.gbl...[color=green]
>> 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[/color]
> directory,[color=green]
>> to prevent a false negative. However, you aren't doing anything to[/color]
> indicate[color=green]
>> 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]
>> - mvp@spam.guard.caspershouse.com
>>
>> "Dan" <dan@dontspamme.com> wrote in message
>> news:OsLAPhzlEHA.3452@TK2MSFTNGP15.phx.gbl...[color=darkred]
>> >I have code like the following to test for existence of a file. I know[/color][/color]
> the[color=green][color=darkred]
>> > 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[/color][/color]
> to[color=green][color=darkred]
>> > 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");
>> >
>> >[/color]
>>
>>[/color]
>
>[/color]


Nicholas Paldino [.NET/C# MVP]
Guest
 
Posts: n/a
#13: Nov 16 '05

re: File.Exists question


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]
- mvp@spam.guard.caspershouse.com

"Dan" <dan@dontspamme.com> wrote in message
news:uhSo08zlEHA.1652@TK2MSFTNGP09.phx.gbl...[color=blue]
> 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]" <mvp@spam.guard.caspershouse.com> wrote
> in
> message news:%23s$fXozlEHA.2884@TK2MSFTNGP09.phx.gbl...[color=green]
>> 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[/color]
> directory,[color=green]
>> to prevent a false negative. However, you aren't doing anything to[/color]
> indicate[color=green]
>> 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]
>> - mvp@spam.guard.caspershouse.com
>>
>> "Dan" <dan@dontspamme.com> wrote in message
>> news:OsLAPhzlEHA.3452@TK2MSFTNGP15.phx.gbl...[color=darkred]
>> >I have code like the following to test for existence of a file. I know[/color][/color]
> the[color=green][color=darkred]
>> > 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[/color][/color]
> to[color=green][color=darkred]
>> > 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");
>> >
>> >[/color]
>>
>>[/color]
>
>[/color]


Dennis Myrén
Guest
 
Posts: n/a
#14: Nov 16 '05

re: File.Exists question


Are you completely sure the file name is correct?


--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Dan" <dan@dontspamme.com> wrote in message
news:%23qNps$zlEHA.1652@TK2MSFTNGP09.phx.gbl...[color=blue]
> But I'm running with administrative rights, with full access to the files[/color]
in[color=blue]
> that directory. Could it have something to do with running from the
> debugger?
>
> "Willy Denoyette [MVP]" <willy.denoyette@pandora.be> wrote in message
> news:OWroB6zlEHA.2864@TK2MSFTNGP14.phx.gbl...[color=green]
> > If you (the windows identity running the code) don't have the right
> > permissions to the folder holding the file, File.Exists will return[/color][/color]
false,[color=blue][color=green]
> > 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[/color]
> security"[color=green]
> > , this kind of security mechanism is based on the "code identity" (where[/color]
> did[color=green]
> > the code came from - internet, intranet, codegroup , ...) NOT the[/color][/color]
windows[color=blue][color=green]
> > user identity, these are fundamentally different. Whatever you do in[/color][/color]
code[color=blue][color=green]
> > (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" <dan@dontspamme.com> wrote in message
> > news:OsLAPhzlEHA.3452@TK2MSFTNGP15.phx.gbl...[color=darkred]
> > >I have code like the following to test for existence of a file. I know[/color][/color]
> the[color=green][color=darkred]
> > > file is there, but File.Exists returns FALSE. The problem appears to[/color][/color][/color]
be[color=blue][color=green][color=darkred]
> > > that the file is in a directory beneath "My Documents". When I move[/color][/color][/color]
it[color=blue]
> to[color=green][color=darkred]
> > > a
> > > directory directly under the root, File.Exists returns TRUE. So I[/color][/color][/color]
tried[color=blue][color=green][color=darkred]
> > > using FileIOPermission to give me rights to read it, but still no[/color][/color][/color]
luck.[color=blue][color=green][color=darkred]
> > > 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 +[/color][/color][/color]
"\n\n");[color=blue][color=green][color=darkred]
> > >
> > >[/color]
> >
> >[/color]
>
>[/color]


Dennis Myrén
Guest
 
Posts: n/a
#15: Nov 16 '05

re: File.Exists question


Are you completely sure the file name is correct?


--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Dan" <dan@dontspamme.com> wrote in message
news:%23qNps$zlEHA.1652@TK2MSFTNGP09.phx.gbl...[color=blue]
> But I'm running with administrative rights, with full access to the files[/color]
in[color=blue]
> that directory. Could it have something to do with running from the
> debugger?
>
> "Willy Denoyette [MVP]" <willy.denoyette@pandora.be> wrote in message
> news:OWroB6zlEHA.2864@TK2MSFTNGP14.phx.gbl...[color=green]
> > If you (the windows identity running the code) don't have the right
> > permissions to the folder holding the file, File.Exists will return[/color][/color]
false,[color=blue][color=green]
> > 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[/color]
> security"[color=green]
> > , this kind of security mechanism is based on the "code identity" (where[/color]
> did[color=green]
> > the code came from - internet, intranet, codegroup , ...) NOT the[/color][/color]
windows[color=blue][color=green]
> > user identity, these are fundamentally different. Whatever you do in[/color][/color]
code[color=blue][color=green]
> > (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" <dan@dontspamme.com> wrote in message
> > news:OsLAPhzlEHA.3452@TK2MSFTNGP15.phx.gbl...[color=darkred]
> > >I have code like the following to test for existence of a file. I know[/color][/color]
> the[color=green][color=darkred]
> > > file is there, but File.Exists returns FALSE. The problem appears to[/color][/color][/color]
be[color=blue][color=green][color=darkred]
> > > that the file is in a directory beneath "My Documents". When I move[/color][/color][/color]
it[color=blue]
> to[color=green][color=darkred]
> > > a
> > > directory directly under the root, File.Exists returns TRUE. So I[/color][/color][/color]
tried[color=blue][color=green][color=darkred]
> > > using FileIOPermission to give me rights to read it, but still no[/color][/color][/color]
luck.[color=blue][color=green][color=darkred]
> > > 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 +[/color][/color][/color]
"\n\n");[color=blue][color=green][color=darkred]
> > >
> > >[/color]
> >
> >[/color]
>
>[/color]


Willy Denoyette [MVP]
Guest
 
Posts: n/a
#16: Nov 16 '05

re: File.Exists question


Ok, make sure the file path is correct and/or check if the administrators
group has access to the folder.

Willy.

"Dan" <dan@dontspamme.com> wrote in message
news:%23qNps$zlEHA.1652@TK2MSFTNGP09.phx.gbl...[color=blue]
> 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?
>[/color]


Willy Denoyette [MVP]
Guest
 
Posts: n/a
#17: Nov 16 '05

re: File.Exists question


Ok, make sure the file path is correct and/or check if the administrators
group has access to the folder.

Willy.

"Dan" <dan@dontspamme.com> wrote in message
news:%23qNps$zlEHA.1652@TK2MSFTNGP09.phx.gbl...[color=blue]
> 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?
>[/color]


Dan
Guest
 
Posts: n/a
#18: Nov 16 '05

re: File.Exists question


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.Req uestMinimum, Name =
"FullTrust")]


"Willy Denoyette [MVP]" <willy.denoyette@pandora.be> wrote in message
news:u7IJBU0lEHA.2764@TK2MSFTNGP11.phx.gbl...[color=blue]
> Ok, make sure the file path is correct and/or check if the administrators
> group has access to the folder.
>
> Willy.
>
> "Dan" <dan@dontspamme.com> wrote in message
> news:%23qNps$zlEHA.1652@TK2MSFTNGP09.phx.gbl...[color=green]
> > But I'm running with administrative rights, with full access to the[/color][/color]
files[color=blue][color=green]
> > in
> > that directory. Could it have something to do with running from the
> > debugger?
> >[/color]
>
>[/color]



Dan
Guest
 
Posts: n/a
#19: Nov 16 '05

re: File.Exists question


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.Req uestMinimum, Name =
"FullTrust")]


"Willy Denoyette [MVP]" <willy.denoyette@pandora.be> wrote in message
news:u7IJBU0lEHA.2764@TK2MSFTNGP11.phx.gbl...[color=blue]
> Ok, make sure the file path is correct and/or check if the administrators
> group has access to the folder.
>
> Willy.
>
> "Dan" <dan@dontspamme.com> wrote in message
> news:%23qNps$zlEHA.1652@TK2MSFTNGP09.phx.gbl...[color=green]
> > But I'm running with administrative rights, with full access to the[/color][/color]
files[color=blue][color=green]
> > in
> > that directory. Could it have something to do with running from the
> > debugger?
> >[/color]
>
>[/color]



Closed Thread


Similar C# / C Sharp bytes