FileIOPermission question

S

schaf

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;
}
 
I

Ignacio Machin \( .NET/ C# MVP \)

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 said:
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;
}
 
S

schaf

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 said:
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;
}
 
T

Tom Spink

schaf said:
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 said:
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
 
W

Willy Denoyette [MVP]

| 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/default.asp?url=/library/en-us/dnpag2/html/PAGPractices0001.asp
to get an high level picture before you dive into the details.


Willy.
 
S

schaf

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 said:
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


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
 
T

Tom Spink

schaf said:
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 said:
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


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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top