473,396 Members | 2,023 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

File.Exists

sur
Hello,

My problem is that File.Exists works fine if my file is on my local
drive but returns false if its on any other drive. I think that the
issue is probably file permissions and so I have tried the following:

FileIOPermision permFileIO = new
FileIOPermission(FileIOPermissionAccess.Read,
filepath.Value.ToString());

try
{
permFileIO.Demand();
}
catch(Exception e)
{
}

This works but how do I catch an exception of the user not having the
right permissions to read the file?

Thanks
Sur

Nov 17 '05 #1
11 9549
> My problem is that File.Exists works fine if my file is on my local
drive but returns false if its on any other drive. I think that the
issue is probably file permissions and so I have tried the following:

FileIOPermision permFileIO = new
FileIOPermission(FileIOPermissionAccess.Read,
filepath.Value.ToString());

try
{
permFileIO.Demand();
}
catch(Exception e) {
}

This works but how do I catch an exception of the user not having the
right permissions to read the file?


Replace the Exception to the exception you want to catch like this:

try {
permFileIO.Demand();
} catch(MyExceptionIWantToCatch e) {
// error
}

It might be that the program have no LAN or Internet access rights
(resposibility of the programmer), but also that the administrator on that
computer does not like the program to access the LAN or Internet drive. I
believe that this one is the one you need
System.Security.Policy.PolicyException

try {
permFileIO.Demand();
} catch(PolicyException e) {
// Report not enough user rights on this machine
}

Technically I believe in C# you might want to nest exceptions
try {
try {
permFileIO.Demand();
} catch(PolicyException e) {
// Report not enough user rights on this machine
}
}catch(Exception e) {
// Typical exception error reporting
}
}

But you might also use like this fall-through way of doing it:
If the exception was PolicyException then the second part is not executed.
But if it was not PolicyException then Exception is executed

try {
try {
permFileIO.Demand();
} catch(PolicyException e) {
// Report not enough user rights on this machine
} catch(Exception e) {
// Typical exception error reporting
}
}

(I hope I wrote it correctly)

Nov 17 '05 #2

"sur" <su************@hotmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Hello,

My problem is that File.Exists works fine if my file is on my local
drive but returns false if its on any other drive. I think that the
issue is probably file permissions and so I have tried the following:

FileIOPermision permFileIO = new
FileIOPermission(FileIOPermissionAccess.Read,
filepath.Value.ToString());

try
{
permFileIO.Demand();
}
catch(Exception e)
{
}

This works but how do I catch an exception of the user not having the
right permissions to read the file?

Thanks
Sur


You are confusing (and so does Olaf), Windows security and Code Access
Security, your problem is related to the first, that is, the USER running
the code has no permissions to access the resource (network share/file).
Access to a network share is controlled by the Windows security system
(through the USER's access token) and as such is a Windows security issue.
FileIOPermission is a CAS permission, here it's the CLR's security system
that checks whether the CODE has been granted FileIO permissions. While both
are totally different, they complement each other, so now you can have 'Code
access' and 'User access' control to a resource.

Willy.
Nov 17 '05 #3
> You are confusing (and so does Olaf), Windows security and Code Access
Security, your problem is related to the first, that is, the USER running
the code has no permissions to access the resource (network share/file).
Access to a network share is controlled by the Windows security system
(through the USER's access token) and as such is a Windows security issue.
FileIOPermission is a CAS permission, here it's the CLR's security system
that checks whether the CODE has been granted FileIO permissions. While both are totally different, they complement each other, so now you can have 'Code access' and 'User access' control to a resource.

I was talking about the .NET administrator that does not give LAN rights to
let the .NET program access the network folder.
Of course you also have to take into account that a Shared folder must have
enough share access rights and that the file/folder permissions on that
machine are set in such a way that you could read that folder.

I was assuming that his windows permissions and share rights were ok,
because that is the first to check maybe by opening the file with notepad.

We have 4 locations that could give the problem:
1. The file/folder have not enough Windows permission.
-> administrator of the shared machine
2. The file/folder share have not enoygh permission.
-> administrator of the shared machine
3. The programmer compiling the managed program forgot to tell the program
that
is should be able to access shared LAN folders.
4. The Administrator of the machine the program must run on should give the
manage
programs rights to go and look for a shared LAN folder.

Most programmers fail point 3 and 4 since they do not know that these are
new for them and should be taken into account.

Since in his code sample he is trying to dynamically demand read access, and
he gets an exception, that would suggest that the program running on that
have no rights to access a shared LAN driver, so it is up to the
administrator on that running machine to tell the .NET CLI is to be trusted
and is allowed to have access. Default it is turned off, so this explain's
why it works locally but not remotely.

But I admit it is sometimes confusing to understand tihis all.


Nov 17 '05 #4

"Olaf Baeyens" <ol**********@skyscan.be> wrote in message
news:43***********************@news.skynet.be...
You are confusing (and so does Olaf), Windows security and Code Access
Security, your problem is related to the first, that is, the USER running
the code has no permissions to access the resource (network share/file).
Access to a network share is controlled by the Windows security system
(through the USER's access token) and as such is a Windows security
issue.
FileIOPermission is a CAS permission, here it's the CLR's security system
that checks whether the CODE has been granted FileIO permissions. While

both
are totally different, they complement each other, so now you can have

'Code
access' and 'User access' control to a resource.

I was talking about the .NET administrator that does not give LAN rights
to
let the .NET program access the network folder.
Of course you also have to take into account that a Shared folder must
have
enough share access rights and that the file/folder permissions on that
machine are set in such a way that you could read that folder.

I was assuming that his windows permissions and share rights were ok,
because that is the first to check maybe by opening the file with notepad.

We have 4 locations that could give the problem:
1. The file/folder have not enough Windows permission.
-> administrator of the shared machine
2. The file/folder share have not enoygh permission.
-> administrator of the shared machine
3. The programmer compiling the managed program forgot to tell the program
that
is should be able to access shared LAN folders.
4. The Administrator of the machine the program must run on should give
the
manage
programs rights to go and look for a shared LAN folder.

Most programmers fail point 3 and 4 since they do not know that these are
new for them and should be taken into account.

Since in his code sample he is trying to dynamically demand read access,
and
he gets an exception, that would suggest that the program running on that
have no rights to access a shared LAN driver, so it is up to the
administrator on that running machine to tell the .NET CLI is to be
trusted
and is allowed to have access. Default it is turned off, so this explain's
why it works locally but not remotely.

But I admit it is sometimes confusing to understand tihis all.


No, a program that is loaded from a local drive runs at "full trust" by
default. That means that the "code" already runs with FileIOPermission no
matter the location of the file, the CAS security doesn't care about the
location, unless the OP explicitly denied access to the remote file or
directory, something I guess he didn't do.
That is why the OP's program succeeds when executing File.Exists for a local
file, but the reason why it fails to access a file on a shared drive is
because the "user" has no access to the shared resource.
Note that the OP got "the exception" before he added the code he posted, but
unfortunately failed to post the exception message, something that leads to
more confusion and endless discussion.

Willy.


Nov 17 '05 #5
> No, a program that is loaded from a local drive runs at "full trust" by
default. That means that the "code" already runs with FileIOPermission no
matter the location of the file, the CAS security doesn't care about the
location, unless the OP explicitly denied access to the remote file or
directory, something I guess he didn't do.


Odd, if I understand your argument, then it means that if I start up a .NET
program locally on my C: drive then CLI does not prevents me from accessing
a shared network file by default way?
I mean suppose, take a default Windows installation, create a default C#
form with a button to open a file, and it will, without protest open a
shared file when I start up the program on my C: drive?

Either something changed between .NET v1.0 to v1.1 or my tests here are
wrong, since it took me the hell of a time to find out how to make an
installer that automatically makes my program, installed locally to open a
shared folder witout complaint.

Maybe we are talking about the same thing here but somehow have a
communication problem.
Here my last try:

The program have the programmers .NET *permission* to open a shared folder
file, but the administrator of that local machine might have a .NET *policy*
not to let that program do that.
And I was always convinced that the default .NET installation has it's
*policy* never to allow a .NET program, even run locally have access t a
shared network folder/file by default.
You must activate this exceptional *policy* access by telling the .NET
framework to let the program with this key signature and maybe including
this version info to grant access to open a shared folder/file.
through this: "Control panel->Administrative Tools->Microsoft .NET Framework
1.1 Wizards->Trust an assembly"

Changing the global default trust is not a wise choice.

But since this is a scary thing for most users, this could be done
automatically through a setup project which is a pure unmanaged program, and
have thus rights to access the using a custom installer inherited from
System.Configuration.Install.Installer

[RunInstaller(true)]
public class Installer1 : System.Configuration.Install.Installer {
...
}

Willy were we talking about the same thing?
Or am I completely and officially confused? ;-)

I agree with you, it could be plain old Windows network access rights, but
my bet is on the .NET policy thing since this is new and newbie programmers
all fall for this one.
Nov 17 '05 #6
Olaf Baeyens wrote:
No, a program that is loaded from a local drive runs at "full trust" by
default. That means that the "code" already runs with FileIOPermission no
matter the location of the file, the CAS security doesn't care about the
location, unless the OP explicitly denied access to the remote file or
directory, something I guess he didn't do.


Odd, if I understand your argument, then it means that if I start up a .NET
program locally on my C: drive then CLI does not prevents me from accessing
a shared network file by default way?
I mean suppose, take a default Windows installation, create a default C#
form with a button to open a file, and it will, without protest open a
shared file when I start up the program on my C: drive?


That certainly seems to work for me - I've just tried it.

Jon

Nov 17 '05 #7
> > Odd, if I understand your argument, then it means that if I start up a
..NET
program locally on my C: drive then CLI does not prevents me from accessing a shared network file by default way?
I mean suppose, take a default Windows installation, create a default C#
form with a button to open a file, and it will, without protest open a
shared file when I start up the program on my C: drive?


That certainly seems to work for me - I've just tried it.

Interesting I will have to test this again when I find time.
It could simplify the install procedure even more.

I assume we are using .NET framework v1.1?
Nov 17 '05 #8
> > > I mean suppose, take a default Windows installation, create a default
C#
form with a button to open a file, and it will, without protest open a
shared file when I start up the program on my C: drive?


That certainly seems to work for me - I've just tried it.

Interesting I will have to test this again when I find time.
It could simplify the install procedure even more.

I assume we are using .NET framework v1.1?

I just checked, and to my big supprise the pure C# program even executes
from a shared drive.
But it refuses to open a shared folder.

When I copy this test program locally then it starts up and to my suprise it
also opens the shared folder.

I am 100% sure that this did not happen 2 years ago when I was testing with
VC# 2002 and the .NET framework v1.0.
What changed?

Very curious. 8-)
Anyway, I am glad that it works like this, so distribution is now even
simpler :-)

Nov 17 '05 #9

"Olaf Baeyens" <ol**********@skyscan.be> wrote in message
news:43*********************@news.skynet.be...
> > I mean suppose, take a default Windows installation, create a default C# > > form with a button to open a file, and it will, without protest open
> > a
> > shared file when I start up the program on my C: drive?
>
> That certainly seems to work for me - I've just tried it.
>

Interesting I will have to test this again when I find time.
It could simplify the install procedure even more.

I assume we are using .NET framework v1.1?

I just checked, and to my big supprise the pure C# program even executes
from a shared drive.
But it refuses to open a shared folder.

When I copy this test program locally then it starts up and to my suprise
it
also opens the shared folder.

I am 100% sure that this did not happen 2 years ago when I was testing
with
VC# 2002 and the .NET framework v1.0.
What changed?

Very curious. 8-)
Anyway, I am glad that it works like this, so distribution is now even
simpler :-)


AFAIK it has always been like this, it's quite simple, or the code has
FileIOPermission or it doesn't. The CLR security system has no idea what a
shared resource is, there is no way to make the distinction at the
application level , it's only the OS that knows that a file is not local and
needs the help of the Filesystem redirector to access the file over the
network.

Willy.
Nov 17 '05 #10
AFAIK it has always been like this, it's quite simple, or the code has
FileIOPermission or it doesn't. The CLR security system has no idea what a
shared resource is, there is no way to make the distinction at the
application level , it's only the OS that knows that a file is not local and needs the help of the Filesystem redirector to access the file over the
network.

Odd, I guess that I have been testing by executing from a shared folder and
somehow got convinced that this was also the case when copied locally.
Well I learned a lot more now. :-)
Nov 17 '05 #11


*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #12

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

Similar topics

15
by: Geiregat Jonas | last post by:
is using if(open("file",O_EXCL) != -1){ printf("File does exists")}else{printf("file does not exists"); } a good way of checking if a file exists or not, if not how should I do it ?
3
by: StGo | last post by:
How can i read/write file's custom attributs(like subject,author...) in C#??? Thanks :))
18
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...
2
by: Zeno Lee | last post by:
I'm using File.Exists to test a file on my C: drive. My program was strongly named and had caspol -af run on it to allow it to run from the network. There are 3 ways I am doing this: 1) Run...
2
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...
4
by: DEWright_CA | last post by:
I am trying to see if a file exists in a virtual directory, and if so run a method. I try doing File.Exists and the method runs but the file isn't there. Is there a web version of File.Exists or...
52
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...
17
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: ...
12
by: snow | last post by:
Hi All, I noticed if file path has a white space, for example "C:\my document \test.txt", the function File.Exists(filePath) always return false in release mode. How could I make this function...
7
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
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...
0
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,...

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.