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

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

Files are located in the folder C: \ Testing \ Docs

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

A permission request of type 'System.Security.Permissions.FileIOPermission, mscorlib, Version = 4.0.0.0, Culture = neutral, PublicKeyToken = b77a5c561934e089' failed.

File permissions are set for full access, and I run it as an administrator.

Is there any way?

Thanks.

+4
source share
3 answers

If you deploy the ClickOnce application, an error appears because you do not need an appropriate level of trust to view files. This is different from file permissions.

You can solve this problem in one of the following ways:

  • Add the following attribute to your program:

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

+3
source

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

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

You may also have problems if the program is running from a network share or if it loads an assembly from a network share (although I think some of these rules have changed over the course of .Net 2 life).

Google.net "full trust" - or, in fact, the exception you receive - you will receive a response.

Like this: System.Security.Permissions.FileIOPermission when using MEF to load dll

(Update)

Since this is not an Asp.Net application, you may need to request permission for a named permission set — request a set of "FullTrust" as shown in the example.

However, I think that something is not said there about the application - because, in my opinion, if it is built and launched from your machine, you will not need to do this.

If, for example, exe (or the DLL requesting this permission) was copied to the target computer from an unreliable network location, then it could be blocked by the OS - in this case it will force to work in partial trust. You can try the steps described in this article on the semiforums , check each of the binary files in the application folder to make sure that none of them are locked and, if so, unlock them.

Then try to start it again.

+3
source

This is a web application or a Winforms / Cosole application. For a web application, the user will not be Admin, but the user whose privileges are granted by IIS. You may need to grant user / role rights over a folder or file.

+1
source

All Articles