473,691 Members | 2,468 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

File.Exists question

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 FileIOPermissio n to give me rights to read it, but still no luck. Any
suggestions? Thanks...

FileIOPermissio n f = new
FileIOPermissio n(FileIOPermiss ionAccess.AllAc cess, fileName);
if (!File.Exists(f ileName))
throw new FileNotFoundExc eption("Unable to construct
BinaryUploadFil e object. File does not exist:\n\n" + fileName + "\n\n");
Nov 16 '05 #1
18 3170
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 SecurityExcepti on if you don't have access to the directory.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Dan" <da*@dontspamme .com> wrote in message
news:Os******** ******@TK2MSFTN GP15.phx.gbl...
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 FileIOPermissio n to give me rights to read it, but still no luck.
Any
suggestions? Thanks...

FileIOPermissio n f = new
FileIOPermissio n(FileIOPermiss ionAccess.AllAc cess, fileName);
if (!File.Exists(f ileName))
throw new FileNotFoundExc eption("Unable to construct
BinaryUploadFil e object. File does not exist:\n\n" + fileName + "\n\n");

Nov 16 '05 #2
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 SecurityExcepti on if you don't have access to the directory.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Dan" <da*@dontspamme .com> wrote in message
news:Os******** ******@TK2MSFTN GP15.phx.gbl...
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 FileIOPermissio n to give me rights to read it, but still no luck.
Any
suggestions? Thanks...

FileIOPermissio n f = new
FileIOPermissio n(FileIOPermiss ionAccess.AllAc cess, fileName);
if (!File.Exists(f ileName))
throw new FileNotFoundExc eption("Unable to construct
BinaryUploadFil e object. File does not exist:\n\n" + fileName + "\n\n");

Nov 16 '05 #3
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" <da*@dontspamme .com> wrote in message
news:Os******** ******@TK2MSFTN GP15.phx.gbl...
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 FileIOPermissio n to give me rights to read it, but still no luck.
Any
suggestions? Thanks...

FileIOPermissio n f = new
FileIOPermissio n(FileIOPermiss ionAccess.AllAc cess, fileName);
if (!File.Exists(f ileName))
throw new FileNotFoundExc eption("Unable to construct
BinaryUploadFil e object. File does not exist:\n\n" + fileName + "\n\n");

Nov 16 '05 #4
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" <da*@dontspamme .com> wrote in message
news:Os******** ******@TK2MSFTN GP15.phx.gbl...
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 FileIOPermissio n to give me rights to read it, but still no luck.
Any
suggestions? Thanks...

FileIOPermissio n f = new
FileIOPermissio n(FileIOPermiss ionAccess.AllAc cess, fileName);
if (!File.Exists(f ileName))
throw new FileNotFoundExc eption("Unable to construct
BinaryUploadFil e object. File does not exist:\n\n" + fileName + "\n\n");

Nov 16 '05 #5
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?

FileIOPermissio n f = new
FileIOPermissio n(FileIOPermiss ionAccess.AllAc cess, fileName);
f.Demand();
if (!File.Exists(f ileName))
throw new FileNotFoundExc eption("Unable to construct
BinaryUploadFil e object. File does not exist:\n\n" + fileName + "\n\n");
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> wrote in
message news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
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 SecurityExcepti on if you don't have access to the directory.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Dan" <da*@dontspamme .com> wrote in message
news:Os******** ******@TK2MSFTN GP15.phx.gbl...
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 FileIOPermissio n to give me rights to read it, but still no luck.
Any
suggestions? Thanks...

FileIOPermissio n f = new
FileIOPermissio n(FileIOPermiss ionAccess.AllAc cess, fileName);
if (!File.Exists(f ileName))
throw new FileNotFoundExc eption("Unable to construct
BinaryUploadFil e object. File does not exist:\n\n" + fileName + "\n\n");


Nov 16 '05 #6
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?

FileIOPermissio n f = new
FileIOPermissio n(FileIOPermiss ionAccess.AllAc cess, fileName);
f.Demand();
if (!File.Exists(f ileName))
throw new FileNotFoundExc eption("Unable to construct
BinaryUploadFil e object. File does not exist:\n\n" + fileName + "\n\n");
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> wrote in
message news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
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 SecurityExcepti on if you don't have access to the directory.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Dan" <da*@dontspamme .com> wrote in message
news:Os******** ******@TK2MSFTN GP15.phx.gbl...
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 FileIOPermissio n to give me rights to read it, but still no luck.
Any
suggestions? Thanks...

FileIOPermissio n f = new
FileIOPermissio n(FileIOPermiss ionAccess.AllAc cess, fileName);
if (!File.Exists(f ileName))
throw new FileNotFoundExc eption("Unable to construct
BinaryUploadFil e object. File does not exist:\n\n" + fileName + "\n\n");


Nov 16 '05 #7
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]" <wi************ *@pandora.be> wrote in message
news:OW******** ******@TK2MSFTN GP14.phx.gbl...
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" <da*@dontspamme .com> wrote in message
news:Os******** ******@TK2MSFTN GP15.phx.gbl...
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 FileIOPermissio n to give me rights to read it, but still no luck.
Any
suggestions? Thanks...

FileIOPermissio n f = new
FileIOPermissio n(FileIOPermiss ionAccess.AllAc cess, fileName);
if (!File.Exists(f ileName))
throw new FileNotFoundExc eption("Unable to construct
BinaryUploadFil e object. File does not exist:\n\n" + fileName + "\n\n");


Nov 16 '05 #8
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]" <wi************ *@pandora.be> wrote in message
news:OW******** ******@TK2MSFTN GP14.phx.gbl...
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" <da*@dontspamme .com> wrote in message
news:Os******** ******@TK2MSFTN GP15.phx.gbl...
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 FileIOPermissio n to give me rights to read it, but still no luck.
Any
suggestions? Thanks...

FileIOPermissio n f = new
FileIOPermissio n(FileIOPermiss ionAccess.AllAc cess, fileName);
if (!File.Exists(f ileName))
throw new FileNotFoundExc eption("Unable to construct
BinaryUploadFil e object. File does not exist:\n\n" + fileName + "\n\n");


Nov 16 '05 #9
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]
- mv*@spam.guard. caspershouse.co m

"Dan" <da*@dontspamme .com> wrote in message
news:uh******** ******@TK2MSFTN GP09.phx.gbl...
Thanks Nick, but it didn't seem to work. Changed the code to this (see
below), but Exists still returned false. Any more ideas?

FileIOPermissio n f = new
FileIOPermissio n(FileIOPermiss ionAccess.AllAc cess, fileName);
f.Demand();
if (!File.Exists(f ileName))
throw new FileNotFoundExc eption("Unable to construct
BinaryUploadFil e object. File does not exist:\n\n" + fileName + "\n\n");
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> wrote
in
message news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
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 SecurityExcepti on if you don't have access to the directory.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Dan" <da*@dontspamme .com> wrote in message
news:Os******** ******@TK2MSFTN GP15.phx.gbl...
>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 FileIOPermissio n to give me rights to read it, but still no luck.
> Any
> suggestions? Thanks...
>
> FileIOPermissio n f = new
> FileIOPermissio n(FileIOPermiss ionAccess.AllAc cess, fileName);
> if (!File.Exists(f ileName))
> throw new FileNotFoundExc eption("Unable to construct
> BinaryUploadFil e object. File does not exist:\n\n" + fileName +
> "\n\n");
>
>



Nov 16 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
4507
by: Raymond | last post by:
Hi All: To find a file exists using the file name, I have tow routings on UNIX system. 1. access(2) 2. lstat(2) This tow function also can do. When the return value is "-1" and errno is "ENOENT", The file doesn't exist.
2
14399
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."
4
4479
by: Matt Jensen | last post by:
Howdy I've got a rather strange issue occuring. I used forms based .NET authentication, although I'm also setting some session variables when people login. However, I've found when people use one of my webform pages which includes a button that pops up a window where you can upload files, if you upload files in this popup window, it seems to somehow clear out all of the session variables and the users get logged out. However, if...
3
19802
by: Lou | last post by:
Question: I can't seem to get file.exists(filename) to return true when I search using wildcards, and I know there's a file in that dir with that extension. here's the path dim yesno as boolean FileName = "D:\Documents and Settings\Lou\My Documents\Visual Studio Projects\Lucky7\Lucky7\bin\*.txt"
1
2585
by: Tim Failes | last post by:
This seems a trival question, but I cannot get it to work properly... Essentially my question is, how can I create a text file, and guarantee it is given the current date/time as the Creation Time? My code is creating a file correctly, but the CreationTime is (sometimes) set to the CreationTime of an old file that has previously been removed. The line of code creating the file is _LogFileWriter = newFile.CreateText()
52
7510
by: paytam | last post by:
Hi all Can anyone tell me how can I check that a file exist or no.I mean when you use this commands FILE *fp; if(!fp) //Could not open the file doen't show why it can not open it,may be the file doesn't exist.Now tell me what should I do! Thanks
17
8021
by: Peter Duniho | last post by:
I searched using Google, on the web and in the newsgroups, and found nothing on this topic. Hopefully that means I just don't understand what I'm supposed to be doing here. :) The problem: I am trying to use the SaveFileDialog class to get a filename, which is subsequently opened for writing (write access, read sharing, but using read/write sharing doesn't make the problem go away anyway). Sometimes, on the statement where I...
7
19083
by: sprash | last post by:
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!
8
12225
by: Sweetiecakes | last post by:
In our series of "probably something simple"... I am doing a File.Exists operation to check if a file exists. Let's assume that this file is C:/test.txt. Now, if I do File.Exists("C:\\Test.txt"), I do not want it to tell me that the file exists - this is a check for a File.Move operation (some file renaming functionality). But if I check C:\\test.txt, it should return true.
0
8533
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9079
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
8951
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...
1
8791
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8795
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
5813
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
4322
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4552
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2966
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.