Home  >  Article  >  Backend Development  >  File permissions in C#

File permissions in C#

王林
王林forward
2023-09-08 21:29:021031browse

C# 中的文件权限

For file permissions in C#, use the FileIOPermission class. It controls the ability to access files and folders.

The following are the attributes of the file permission class-

1
Sr.No. Methods and instructions
AllFiles

Gets or sets allowed access to all files.

2 AllLocalFiles

Gets or sets allowed access to all local files.

The following are the methods of the file permission class-

##1234
Mr. Methods and descriptions
AddPathList(FileIOPermissionAccess, String)This method Adds access to the specified file or directory to the existing state of permissions

Copy() This method creates and returns an identical copy of the current permissions.

GetType()GetType() method gets the current type Example.

ToXml()Create an XML encoding of permissions and its Current status.

Let’s see an example of using the FileIOPermission class in C#. Here, the Demand() method forces a SecurityException to be thrown at runtime if all callers higher up in the call stack have not been granted the permissions 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);
      }
   }
}

The above is the detailed content of File permissions in C#. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete