File Permissions in C#

For File Permission in C#, use the FileIOPermission Class. It controls the ability to access files and folders.

The following are the properties of File Permissions class −

Sr.No.Methods & Description
Gets or sets the permitted access to all files.
Gets or sets the permitted access to all local files.

The following are the methods of File Permission class −

Sr.No.Methods & Description
1AddPathList(FileIOPermissionAccess, String)
This method adds access for the specified file or directory to the existing state of the permission
This method creates and returns an identical copy of the current permission.
The GetType() method gets the type of the current instance.
Creates an XML encoding of the permission and its current state.

Let us see an example to work wuth FileIOPermission Class in C#. Here, the Demand() method forces a SecurityException at run time if all callers higher in the call stack have not been granted the permission specified by the current instance −


using System;
using System.IO;
using System.Security.Permissions;
using System.Security;

public class Demo {
   public static void Main() {

      FileIOPermission file= new FileIOPermission(PermissionState.None);
      file.AllLocalFiles = FileIOPermissionAccess.Read;
      try {
         Console.WriteLine("Demands the permission to determine whether the application has
         permission to read the files");
      catch (SecurityException s) {