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 |
|---|---|
| 1 | AllFiles Gets or sets the permitted access to all files. |
| 2 | AllLocalFiles Gets or sets the permitted access to all local files. |
The following are the methods of File Permission class −
| Sr.No. | Methods & Description |
|---|---|
| 1 | AddPathList(FileIOPermissionAccess, String) This method adds access for the specified file or directory to the existing state of the permission |
| 2 | Copy() This method creates and returns an identical copy of the current permission. |
| 3 | GetType() The GetType() method gets the type of the current instance. |
| 4 | ToXml() 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 −
Example
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");
file.Demand();
}
catch (SecurityException s) {
Console.WriteLine(s.Message);
}
}
}