4

I have a C# .Net 4.0 Project which needs to read text files and parse them - very simple.

The files are located in C:\Testing\Docs

When I try to open a text file in the above directory I get the following error:

Request for the permission of type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.

The files permissions are set to full access and I am running as administrator.

Is there any way around it?

Thanks.

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
DreX
  • 333
  • 1
  • 5
  • 17

3 Answers3

4

If you're deploying a ClickOnce application, then the error appears because you don't have the appropriate trust level required to view files. This is different from file permissions.

You can solve this in one of the following ways:

  1. Add the following attribute to your program:

    [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
    
  2. Change the trust level in your project properties. The short way is to just check "This is a full trust application", or you can go ahead and add the file permissions manually.

foxy
  • 7,599
  • 2
  • 30
  • 34
  • Where do I add the 1st option? I added it before the method where the error is occurring [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")] private void Parse(string file) { String data = null; StreamReader sr = new StreamReader(file); } but I am getting another error now "Request Failed." at the start of the method. Thanks. – DreX Jun 15 '11 at 09:57
  • @DreX if the request is failing it implies the OS is forcing all or part of the app to be partial trust; I've updated my answer to include some info about file-blocking that might help. – Andras Zoltan Jun 15 '11 at 10:08
  • @Andras add it to your main Program class. You can also determine your application's trust level with the DetermineApplicationTrust() function at http://msdn.microsoft.com/en-us/library/system.security.hostsecuritymanager.aspx – foxy Jun 15 '11 at 10:13
  • @freedompeace - thanks but I think you meant @DreX! :) +1 there for the `DetermineApplicationTrust()` method there – Andras Zoltan Jun 15 '11 at 10:20
  • @Andras ah I did too ! Thank you ! Is there a way to edit comments? – foxy Jun 15 '11 at 10:21
  • @freedompeace - yeah there is; but only for 5 minutes within posting them. Are you seeing the little 'x' next to the comment as you hover over with your mouse? You could copy the comment, delete, and repost? That makes my comment look odd - but importantly at least brings it to the attention of the OP. – Andras Zoltan Jun 15 '11 at 10:31
  • Thank you for your help. I have added it to my main program class and still getting "Request Failed." The application security setting are also set to FullTrust – DreX Jun 15 '11 at 10:31
3

This is not a file system permissions issue - it's about 'trust'.

Is this a web application? If so, you need to increase the trust level.

You might also be having issues if the program is running from a network share, or if it's loading an assembly from a network share (although I think some of those rules changed during .Net 2's lifetime).

Google .net 'full trust' - or, actually, the exception you're getting - you'll reach an answer.

There's this SO too: System.Security.Permissions.FileIOPermission when using MEF to load dll's

(Update)

Since it's not an Asp.Net application - you might need to Request Permission for a Named Permission Set - requesting the "FullTrust" set as per the example given.

However, I think there's something you're not saying about the app - because to my mind if it were being built and run from your machine then you shouldn't need to do this.

If, for example, the exe (or DLL that is requesting this permission) has been copied to the target machine from an untrusted network location, then it might have been blocked by the OS - in which case it will be forced to run in partial trust. You could try the steps outlined on this article on sevenforums, checking each of the binaries in the application folder to make sure none have been blocked and, if so, unblock them.

Then try running it again.

Community
  • 1
  • 1
Andras Zoltan
  • 41,961
  • 13
  • 104
  • 160
1

Is it a Web Application or a Winforms/Cosole Application. For Web Application, the user will not be Admin but the user under whose privileges IIS is executing. You may need to give that user / role rights over the folder or file.

sandyiit
  • 1,597
  • 3
  • 17
  • 23