473,382 Members | 1,421 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,382 software developers and data experts.

FileIOPermission question

Hi NG !
I have the following piece of code below. the first try/catch part
would return true although I do not have permission to access this
computer. The second try/catch part would return false.
Do I misunderstand the meaning of FileIOPermission ?
From MSDN: "Controls the ability to access files and folders"

???
Thanks for education!
Regards Marcel (Schaf)

private bool _HasRemoteAccess() {
string sRemotePath = @"\\192.168.2.2\C$\Temp\Text.txt";
bool bHasAccess = true;

//returns true
try {
FileIOPermission permission = new
FileIOPermission(FileIOPermissionAccess.Write, sRemotePath);
permission.Demand();
} catch (System.Security.SecurityException se) {
bHasAccess = false;
}

//returns false
try {
string[] sRemoteFiles = Directory.GetFiles(sRemotePath);
} catch (Exception e) {
bHasAccess = false;
}
return bHasAccess;
}

Jun 22 '06 #1
6 2447
Hi,

What if you do a Console.Write( e.Message ) ?

It will give you the exact problem with the GetFiles method
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"schaf" <sc***@2wire.ch> wrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
Hi NG !
I have the following piece of code below. the first try/catch part
would return true although I do not have permission to access this
computer. The second try/catch part would return false.
Do I misunderstand the meaning of FileIOPermission ?
From MSDN: "Controls the ability to access files and folders"

???
Thanks for education!
Regards Marcel (Schaf)

private bool _HasRemoteAccess() {
string sRemotePath = @"\\192.168.2.2\C$\Temp\Text.txt";
bool bHasAccess = true;

//returns true
try {
FileIOPermission permission = new
FileIOPermission(FileIOPermissionAccess.Write, sRemotePath);
permission.Demand();
} catch (System.Security.SecurityException se) {
bHasAccess = false;
}

//returns false
try {
string[] sRemoteFiles = Directory.GetFiles(sRemotePath);
} catch (Exception e) {
bHasAccess = false;
}
return bHasAccess;
}

Jun 22 '06 #2

Hi!
What if you do a Console.Write( e.Message ) ?

It will give you the exact problem with the GetFiles method
I do not have problems with the second try and catch, because this acts
like I expect, but the first one should give me a SecurityException
because I have no permission to access the files on this computer. So
to conclude if I test the file access with the FileIOPermission class,
I get the wrong result. But why ?

Regrads
Marcel


--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"schaf" <sc***@2wire.ch> wrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
Hi NG !
I have the following piece of code below. the first try/catch part
would return true although I do not have permission to access this
computer. The second try/catch part would return false.
Do I misunderstand the meaning of FileIOPermission ?
From MSDN: "Controls the ability to access files and folders"

???
Thanks for education!
Regards Marcel (Schaf)

private bool _HasRemoteAccess() {
string sRemotePath = @"\\192.168.2.2\C$\Temp\Text.txt";
bool bHasAccess = true;

//returns true
try {
FileIOPermission permission = new
FileIOPermission(FileIOPermissionAccess.Write, sRemotePath);
permission.Demand();
} catch (System.Security.SecurityException se) {
bHasAccess = false;
}

//returns false
try {
string[] sRemoteFiles = Directory.GetFiles(sRemotePath);
} catch (Exception e) {
bHasAccess = false;
}
return bHasAccess;
}


Jun 23 '06 #3
schaf wrote:

Hi!
What if you do a Console.Write( e.Message ) ?

It will give you the exact problem with the GetFiles method


I do not have problems with the second try and catch, because this acts
like I expect, but the first one should give me a SecurityException
because I have no permission to access the files on this computer. So
to conclude if I test the file access with the FileIOPermission class,
I get the wrong result. But why ?

Regrads
Marcel


--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"schaf" <sc***@2wire.ch> wrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
> Hi NG !
> I have the following piece of code below. the first try/catch part
> would return true although I do not have permission to access this
> computer. The second try/catch part would return false.
> Do I misunderstand the meaning of FileIOPermission ?
>>From MSDN: "Controls the ability to access files and folders"
> ???
> Thanks for education!
> Regards Marcel (Schaf)
>
> private bool _HasRemoteAccess() {
> string sRemotePath = @"\\192.168.2.2\C$\Temp\Text.txt";
> bool bHasAccess = true;
>
> //returns true
> try {
> FileIOPermission permission = new
> FileIOPermission(FileIOPermissionAccess.Write, sRemotePath);
> permission.Demand();
> } catch (System.Security.SecurityException se) {
> bHasAccess = false;
> }
>
> //returns false
> try {
> string[] sRemoteFiles = Directory.GetFiles(sRemotePath);
> } catch (Exception e) {
> bHasAccess = false;
> }
> return bHasAccess;
> }
>


Hi Schaf,

If you inspect the exception that is occurring in the second try...catch
block, you may be surprised. From what I can see, you are trying to call
Directory.GetFiles() on a file. You've defined sRemotePath to be a file,
i.e. "test.txt" and you are using Directory.GetFiles() on that file... I
don't think this will work. You can only list the files of a directory.

That may be why the exception is occurring, in the second try...catch block.

-- Tom Spink
Jun 23 '06 #4

"schaf" <sc***@2wire.ch> wrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
| Hi NG !
| I have the following piece of code below. the first try/catch part
| would return true although I do not have permission to access this
| computer. The second try/catch part would return false.
| Do I misunderstand the meaning of FileIOPermission ?

Yes, you do. FileIOPermission is about Code Access Security (CAS) not about
Windows security. Windows permissions are resource permissions based on
"user" identity (has the user permissions to access the resource?), while
CAS are base on "code" identity (does the "code" have access permissions to
the resource).
Please search the MSDN doc's for more info on the subject, CAS is a complex
issue so you might start with
http://msdn.microsoft.com/library/de...ctices0001.asp
to get an high level picture before you dive into the details.
Willy.
Jun 23 '06 #5
Hi Tom !
Of course you are right, but what I would like to ask is, that with the
current logged on user I do not have permission to access anything on
the computer 192.168.2.2. So why do I get a FileIOPermission = true for
my demand ?
If I test it with the first try/catch then I get true and would fail in
a File.Copy() command. So why true is the return, if I do not have
access on the whole computer ?

Thanks
Regards Schaf
schaf wrote:

Hi!
What if you do a Console.Write( e.Message ) ?

It will give you the exact problem with the GetFiles method


I do not have problems with the second try and catch, because this acts
like I expect, but the first one should give me a SecurityException
because I have no permission to access the files on this computer. So
to conclude if I test the file access with the FileIOPermission class,
I get the wrong result. But why ?

Regrads
Marcel


--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"schaf" <sc***@2wire.ch> wrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
> Hi NG !
> I have the following piece of code below. the first try/catch part
> would return true although I do not have permission to access this
> computer. The second try/catch part would return false.
> Do I misunderstand the meaning of FileIOPermission ?
>>From MSDN: "Controls the ability to access files and folders"
> ???
> Thanks for education!
> Regards Marcel (Schaf)
>
> private bool _HasRemoteAccess() {
> string sRemotePath = @"\\192.168.2.2\C$\Temp\Text.txt";
> bool bHasAccess = true;
>
> //returns true
> try {
> FileIOPermission permission = new
> FileIOPermission(FileIOPermissionAccess.Write, sRemotePath);
> permission.Demand();
> } catch (System.Security.SecurityException se) {
> bHasAccess = false;
> }
>
> //returns false
> try {
> string[] sRemoteFiles = Directory.GetFiles(sRemotePath);
> } catch (Exception e) {
> bHasAccess = false;
> }
> return bHasAccess;
> }
>


Hi Schaf,

If you inspect the exception that is occurring in the second try...catch
block, you may be surprised. From what I can see, you are trying to call
Directory.GetFiles() on a file. You've defined sRemotePath to be a file,
i.e. "test.txt" and you are using Directory.GetFiles() on that file... I
don't think this will work. You can only list the files of a directory.

That may be why the exception is occurring, in the second try...catch block.

-- Tom Spink


Jun 23 '06 #6
schaf wrote:
Hi Tom !
Of course you are right, but what I would like to ask is, that with the
current logged on user I do not have permission to access anything on
the computer 192.168.2.2. So why do I get a FileIOPermission = true for
my demand ?
If I test it with the first try/catch then I get true and would fail in
a File.Copy() command. So why true is the return, if I do not have
access on the whole computer ?

Thanks
Regards Schaf
schaf wrote:
>
> Hi!
>
>> What if you do a Console.Write( e.Message ) ?
>>
>> It will give you the exact problem with the GetFiles method
>
> I do not have problems with the second try and catch, because this acts
> like I expect, but the first one should give me a SecurityException
> because I have no permission to access the files on this computer. So
> to conclude if I test the file access with the FileIOPermission class,
> I get the wrong result. But why ?
>
> Regrads
> Marcel
>
>>
>>
>> --
>> --
>> Ignacio Machin,
>> ignacio.machin AT dot.state.fl.us
>> Florida Department Of Transportation
>>
>>
>> "schaf" <sc***@2wire.ch> wrote in message
>> news:11**********************@m73g2000cwd.googlegr oups.com...
>> > Hi NG !
>> > I have the following piece of code below. the first try/catch part
>> > would return true although I do not have permission to access this
>> > computer. The second try/catch part would return false.
>> > Do I misunderstand the meaning of FileIOPermission ?
>> >>From MSDN: "Controls the ability to access files and folders"
>> > ???
>> > Thanks for education!
>> > Regards Marcel (Schaf)
>> >
>> > private bool _HasRemoteAccess() {
>> > string sRemotePath = @"\\192.168.2.2\C$\Temp\Text.txt";
>> > bool bHasAccess = true;
>> >
>> > //returns true
>> > try {
>> > FileIOPermission permission = new
>> > FileIOPermission(FileIOPermissionAccess.Write, sRemotePath);
>> > permission.Demand();
>> > } catch (System.Security.SecurityException se) {
>> > bHasAccess = false;
>> > }
>> >
>> > //returns false
>> > try {
>> > string[] sRemoteFiles = Directory.GetFiles(sRemotePath);
>> > } catch (Exception e) {
>> > bHasAccess = false;
>> > }
>> > return bHasAccess;
>> > }
>> >


Hi Schaf,

If you inspect the exception that is occurring in the second try...catch
block, you may be surprised. From what I can see, you are trying to call
Directory.GetFiles() on a file. You've defined sRemotePath to be a file,
i.e. "test.txt" and you are using Directory.GetFiles() on that file... I
don't think this will work. You can only list the files of a directory.

That may be why the exception is occurring, in the second try...catch
block.

-- Tom Spink


Hi Scaf,

Unfortunately, FileIOPermission is not what you think it is.
FileIOPermission applies to the code, e.g. is your code allowed to call
FileIO operations. Well, yes, your code IS allowed to call FileIO
operations. But, the result of calling that and being blocked by security
permissions is totally unrelated.

Take a look at the GetAccessControl() method of the FileInfo object.

Hope this helps,
-- Tom Spink
Jun 23 '06 #7

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

Similar topics

0
by: Mircea Pleteriu | last post by:
Hi all, I have this code sample FileIOPermission fp = new FileIOPermission(PermissionState.Unrestricted); fp.Deny(); StreamWriter sw = new StreamWriter(@"c:\test.txt");
0
by: Ant Corrie | last post by:
I am at a loss as to why this is not working. I am trying to setup a TextWriterTraceListener in my application config file and then execute the app from the network. I get a security exception...
0
by: paul | last post by:
I just installed Windows XP SP2 on my web server. Now when I run one of the web applications I get the following error: ArgumentException: Illegal characters in path.] ...
0
by: John Delange | last post by:
Hello, I wrote a .NET Windows.Forms User Control that needs to be runned through Internet Explorer, like in this article: http://www.15seconds.com/issue/030610.htm. This control needs to 1.) Run...
1
by: Marquis | last post by:
I have put the C# Form onito the Internet as an ActiveX Control. However, I cannot do the FileOpen Dialog as I don't have permission, how to solve this problem? Below is the error code:...
3
by: Mike | last post by:
Hi I have problem as folow: Caught Exception: System.Configuration.ConfigurationErrorsException: An error occurred loading a configuration file: Request for the permission of type...
1
by: innes | last post by:
Hi, I'm trying to test permission to access the file system, using FileIOPermission, but can't seem to get it to work. See my sample app below, for an app that tries to demand a permission to...
2
by: Dave | last post by:
Yesterday I posted: **************************** I'm trying to download a file from the Web Server to the user's desktop using myWebClient.DownloadFile as described by Microsoft at...
1
by: Oleg Subachev | last post by:
I try to write some info to file in the CLR Trigger under SQL Server 2005. But I get the following error: System.Security.SecurityException: Request for the permission of type...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.