FileSystemAccessRule 类

定义

表示定义文件或目录的访问规则的访问控制项 (ACE) 的抽象。 此类不能被继承。

public ref class FileSystemAccessRule sealed : System::Security::AccessControl::AccessRule
public sealed class FileSystemAccessRule : System.Security.AccessControl.AccessRule
[System.Security.SecurityCritical]
public sealed class FileSystemAccessRule : System.Security.AccessControl.AccessRule
type FileSystemAccessRule = class
    inherit AccessRule
[<System.Security.SecurityCritical>]
type FileSystemAccessRule = class
    inherit AccessRule
Public NotInheritable Class FileSystemAccessRule
Inherits AccessRule
继承
FileSystemAccessRule
属性

示例

下面的代码示例使用 FileSecurity 类 (ACE) 从文件中添加和删除访问控制项。 你必须提供有效的用户或组帐户以运行此示例。

using System;
using System.IO;
using System.Security.AccessControl;

namespace FileSystemExample
{
    class FileExample
    {
        public static void Main()
        {
            try
            {
                string fileName = "test.xml";

                Console.WriteLine($"Adding access control entry for {fileName}");

                // Add the access control entry to the file.
                AddFileSecurity(fileName, @"DomainName\AccountName",
                    FileSystemRights.ReadData, AccessControlType.Allow);

                Console.WriteLine($"Removing access control entry from {fileName}");

                // Remove the access control entry from the file.
                RemoveFileSecurity(fileName, @"DomainName\AccountName",
                    FileSystemRights.ReadData, AccessControlType.Allow);

                Console.WriteLine("Done.");
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }

        // Adds an ACL entry on the specified file for the specified account.
        public static void AddFileSecurity(string fileName, string account,
            FileSystemRights rights, AccessControlType controlType)
        {
            FileInfo fileInfo = new(fileName);
            FileSecurity fSecurity = fileInfo.GetAccessControl();

            // Add the FileSystemAccessRule to the security settings.
            fSecurity.AddAccessRule(new FileSystemAccessRule(account,
                rights, controlType));

            // Set the new access settings.
            fileInfo.SetAccessControl(fSecurity);
        }

        // Removes an ACL entry on the specified file for the specified account.
        public static void RemoveFileSecurity(string fileName, string account,
            FileSystemRights rights, AccessControlType controlType)
        {
            FileInfo fileInfo = new(fileName);
            FileSecurity fSecurity = fileInfo.GetAccessControl();

            // Remove the FileSystemAccessRule from the security settings.
            fSecurity.RemoveAccessRule(new FileSystemAccessRule(account,
                rights, controlType));

            // Set the new access settings.
            fileInfo.SetAccessControl(fSecurity);
        }
    }
}
Imports System.IO
Imports System.Security.AccessControl

Module FileExample

    Sub Main()
        Try
            Dim fileName As String = "test.xml"

            Console.WriteLine("Adding access control entry for " & fileName)

            ' Add the access control entry to the file.
            AddFileSecurity(fileName, "DomainName\AccountName",
                FileSystemRights.ReadData, AccessControlType.Allow)

            Console.WriteLine("Removing access control entry from " & fileName)

            ' Remove the access control entry from the file.
            RemoveFileSecurity(fileName, "DomainName\AccountName",
                FileSystemRights.ReadData, AccessControlType.Allow)

            Console.WriteLine("Done.")
        Catch e As Exception
            Console.WriteLine(e)
        End Try

    End Sub

    ' Adds an ACL entry on the specified file for the specified account.
    Sub AddFileSecurity(ByVal fileName As String, ByVal account As String,
        ByVal rights As FileSystemRights, ByVal controlType As AccessControlType)

        Dim fileInfo As New FileInfo(fileName)
        Dim fSecurity As FileSecurity = fileInfo.GetAccessControl()

        ' Add the FileSystemAccessRule to the security settings. 
        Dim accessRule As New FileSystemAccessRule(account, rights, controlType)

        fSecurity.AddAccessRule(accessRule)

        ' Set the new access settings.
        fileInfo.SetAccessControl(fSecurity)

    End Sub

    ' Removes an ACL entry on the specified file for the specified account.
    Sub RemoveFileSecurity(ByVal fileName As String, ByVal account As String,
        ByVal rights As FileSystemRights, ByVal controlType As AccessControlType)

        Dim fileInfo As New FileInfo(fileName)
        Dim fSecurity As FileSecurity = fileInfo.GetAccessControl()

        ' Remove the FileSystemAccessRule from the security settings. 
        fSecurity.RemoveAccessRule(New FileSystemAccessRule(account,
            rights, controlType))

        ' Set the new access settings.
        fileInfo.SetAccessControl(fSecurity)

    End Sub
End Module

注解

FileSystemAccessRule 表示基础访问控制项 (ACE) 的抽象,该条目指定用户帐户、提供 (读取、写入等) 的访问类型,以及是允许还是拒绝该权限。 此类还可以指定如何将访问规则传播到子对象。

FileSystemAccessRule使用 类创建新的访问规则。 可以使用 或 DirectorySecurity 类保留规则FileSecurity

构造函数

FileSystemAccessRule(IdentityReference, FileSystemRights, AccessControlType)

使用以下内容初始化 FileSystemAccessRule 类的新实例:对用户帐户的引用、指定与访问规则关联的操作的类型的值,以及指定是允许还是拒绝该操作的值。

FileSystemAccessRule(IdentityReference, FileSystemRights, InheritanceFlags, PropagationFlags, AccessControlType)

使用以下内容初始化 FileSystemAccessRule 类的新实例:对用户帐户的引用、指定与访问规则关联的操作的类型的值、确定如何继承权限的值、确定如何传播权限的值,以及指定是允许还是拒绝该操作的值。

FileSystemAccessRule(String, FileSystemRights, AccessControlType)

使用以下内容初始化 FileSystemAccessRule 类的新实例:用户帐户的名称、指定与访问规则关联的操作的类型的值,以及描述是允许还是拒绝该操作的值。

FileSystemAccessRule(String, FileSystemRights, InheritanceFlags, PropagationFlags, AccessControlType)

使用以下内容初始化 FileSystemAccessRule 类的新实例:用户帐户的名称、指定与访问规则关联的操作的类型的值、确定如何继承权限的值、确定如何传播权限的值,以及指定是允许还是拒绝该操作的值。

属性

AccessControlType

获取与此 AccessControlType 对象关联的 AccessRule 对象。

(继承自 AccessRule)
AccessMask

获取此规则的访问掩码。

(继承自 AuthorizationRule)
FileSystemRights

获取与当前 FileSystemRights 对象关联的 FileSystemAccessRule 标志。

IdentityReference

获取对其应用此规则的 IdentityReference

(继承自 AuthorizationRule)
InheritanceFlags

获取用于确定子对象如何继承此规则的标志的值。

(继承自 AuthorizationRule)
IsInherited

获取一个值,该值指示此规则是否为显式设置或继承自父级容器对象。

(继承自 AuthorizationRule)
PropagationFlags

获取传播标志的值,该值确定如何将此规则的继承传播到子对象。 仅当 InheritanceFlags 枚举的值不为 None 时,此属性才有意义。

(继承自 AuthorizationRule)

方法

Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
ToString()

返回表示当前对象的字符串。

(继承自 Object)

适用于