Search code examples
c#.netfilefile-permissionsstreamreader

Cannot read txt files from C:\Testing\Docs using C#.Net


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.


Solution

  • 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.