Discussion:
FileIOPermission problem
(too old to reply)
Schemer
2006-11-20 14:29:30 UTC
Permalink
Hello,

I am working on a C# console app.
When I do this:

DirectoryInfo di = new DirectoryInfo(".");

I get an exception:
Request for the permission of type
'System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.

I added this before the DirectoryInfo creation:

FileIOPermission f = new FileIOPermission(PermissionState.None);
f.AllLocalFiles = FileIOPermissionAccess.Read;

but the error still occurs.

My app needs to read certain files in the current dir, and then create new
files. How does the app get the permissions to do those things?

Thanks for any insight...
Jeffrey Tan[MSFT]
2006-11-21 04:08:34 UTC
Permalink
Hi Schemer,

Can you provide the full stack trace of this exception? This will reveal
much important information of the failure.

Based on the error message, this exception is most likely of type
SecurityException, which is thrown by .Net Code Access Security engine. The
Net SecurityException is most likely caused by running the application
from a partial trusted location, because CLR will give limited permission
to partial trusted location running application. So, can you provide more
context information of your problem and application? For example, where do
you launch the application, local folder or network share?

It is strange that your application throws SecurityException while
constructing DirectoryInfo for current folder ".", because I have created a
little sample project which calls DirectoryInfo di = new
DirectoryInfo("."). Then I placed this test assembly in a network share
folder and no SecurityException will generate in the test. So, I recommend
you provide more detailed information to help me reproduce the problem.
This will be more efficient to reduce the troubleshooting time. Thanks.

Finally, setting FileIOPermission.AllLocalFiles property to
FileIOPermissionAccess.Read does not mean that it will give file read I/O
permission to all local files. But, it means that you want to check your
application's file read I/O permission to all local files. To explicitly
test this, you should use FileIOPermission.Demand method to force the .Net
runtime security checking. Actually, if this property can be used to grant
FileIOPermission to an application, this will become a big security hole,
because any untrusted application can use this property to acquire
FileIOPermission, which workarounds the .Net CAS engine.

Additionally, there is an easy to understand .Net CAS article in
codeproject, it will provide more useful .Net CAS background information to
you:
"Understanding .NET Code Access Security"
http://www.codeproject.com/dotnet/UB_CAS_NET.asp

I look forward to your further information. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
Willy Denoyette [MVP]
2006-11-21 10:27:01 UTC
Permalink
Post by Schemer
Hello,
I am working on a C# console app.
DirectoryInfo di = new DirectoryInfo(".");
Request for the permission of type 'System.Security.Permissions.FileIOPermission,
mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
FileIOPermission f = new FileIOPermission(PermissionState.None);
f.AllLocalFiles = FileIOPermissionAccess.Read;
but the error still occurs.
My app needs to read certain files in the current dir, and then create new files. How
does the app get the permissions to do those things?
Thanks for any insight...
Do you happen to run this application from a network share?

Willy.
Schemer
2006-11-27 13:29:16 UTC
Permalink
Thanks for the replies.

Unfortunately, I have been unable to reproduce the problem since re-starting
the computer.
I removed references to FileIOPermission. Instead, I create my input
FileStream as
new FileStream(fn, FileMode.Open, FileAccess.Read, FileShare.Read);
and my output stream as either
fsOut = File.Open(fnOut, FileMode.Append);
or
fsOut = File.Open(fnOut, FileMode.Create);

Does the Security tab in the project's options have anything to do with
runtime issues, or just deployment?

Thanks again for the help.
Post by Schemer
I am working on a C# console app.
DirectoryInfo di = new DirectoryInfo(".");
Request for the permission of type
'System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
FileIOPermission f = new FileIOPermission(PermissionState.None);
f.AllLocalFiles = FileIOPermissionAccess.Read;
but the error still occurs.
Jeffrey Tan[MSFT]
2006-11-28 07:27:41 UTC
Permalink
Hi Schemer,

Thanks for your feedback!

What is the setting in "Security" tab of your project? This "Security" tab
is used for debugging ClickOnce application. It can also be used to launch
the application under a limited permission set to simulate the partial
trusted location. However, normally, we will not check "Enable ClickOnce
Security Setttings" checkbox. Do you check it in your project which caused
the CAS exception?

I will wait for your further information. Thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
Loading...