FileSystemRights 枚举

定义

定义创建访问和审核规则时要使用的访问权限。

此枚举支持其成员值的按位组合。

public enum class FileSystemRights
[System.Flags]
public enum FileSystemRights
[System.Flags]
[System.Security.SecurityCritical]
public enum FileSystemRights
[<System.Flags>]
type FileSystemRights = 
[<System.Flags>]
[<System.Security.SecurityCritical>]
type FileSystemRights = 
Public Enum FileSystemRights
继承
FileSystemRights
属性

字段

AppendData 4

指定将数据追加到文件末尾的权利。

ChangePermissions 262144

指定更改与文件或文件夹关联的安全和审核规则的权限。

CreateDirectories 4

指定创建文件夹的权限:此权限需要 Synchronize 值。

CreateFiles 2

指定创建文件的权利。 此权限需要 Synchronize 值。

Delete 65536

指定删除文件夹或文件的权限。

DeleteSubdirectoriesAndFiles 64

指定删除文件夹和该文件夹中包含的任何文件的权限。

ExecuteFile 32

指定运行应用程序文件的权利。

FullControl 2032127

指定对文件夹或文件进行完全控制以及修改访问控制和审核规则的权限。 此值表示对文件执行任何操作的权限,并且是此枚举中所有权限的组合。

ListDirectory 1

指定读取目录内容的权限。

Modify 197055

指定读取、写入、列出文件夹内容、删除文件夹和文件以及运行应用程序文件的权限。 此权限包括 ReadAndExecute 权限、Write 权限和 Delete 权限。

Read 131209

指定以只读格式打开和复制文件夹或文件的权限。 此权限包括 ReadData 权限、ReadExtendedAttributes 权限、ReadAttributes 权限和 ReadPermissions 权限。

ReadAndExecute 131241

指定以只读方式打开和复制文件夹或文件以及运行应用程序文件的权限。 此权限包括 Read 权限和 ExecuteFile 权限。

ReadAttributes 128

指定从文件夹或文件打开和复制文件系统属性的权限。 例如,此值指定查看文件创建或修改日期的权利。 这不包括读取数据、扩展文件系统属性或访问和审核规则的权限。

ReadData 1

指定打开和复制文件或文件夹的权限。 这不包括读取文件系统属性、扩展文件系统属性或访问和审核规则的权限。

ReadExtendedAttributes 8

指定从文件夹或文件打开和复制扩展文件系统属性的权限。 例如,此值指定查看作者和内容信息的权利。 这不包括读取数据、文件系统属性或访问和审核规则的权限。

ReadPermissions 131072

指定从文件夹或文件打开和复制访问和审核规则的权限。 这不包括读取数据、文件系统属性和扩展文件系统属性的权利。

Synchronize 1048576

指定应用程序是否可以等待文件句柄与 I/O 操作完成同步。 允许访问时自动设置此值,并在拒绝访问时自动排除此值。

TakeOwnership 524288

指定更改文件夹或文件的所有者的权限。 请注意,资源的所有者对该资源具有完全访问权限。

Traverse 32

指定列出文件夹内容和运行该文件夹中包含的应用程序的权限。

Write 278

指定创建文件夹和文件的权限,以及添加或删除文件中的数据。 此权限包括 WriteData 权限、AppendData 权限、WriteExtendedAttributes 权限和 WriteAttributes 权限。

WriteAttributes 256

指定打开文件系统属性并将文件系统属性写入文件夹或文件的权限。 这不包括写入数据、扩展属性或访问和审核规则的功能。

WriteData 2

指定打开和写入文件或文件夹的权限。 这不包括打开和写入文件系统属性、扩展文件系统属性或访问和审核规则的权限。

WriteExtendedAttributes 16

指定打开扩展文件系统属性并将扩展文件系统属性写入文件夹或文件的权限。 这不包括写入数据、属性或访问和审核规则的功能。

示例

以下示例使用 FileSystemRights 枚举来指定访问规则,然后从文件中删除访问规则。 必须提供有效的用户或组帐户才能运行此示例。

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

注解

FileSystemRights 枚举指定特定用户帐户允许哪些文件系统操作,以及为特定用户帐户审核哪些文件系统操作。

使用 FileSystemAccessRule 类创建访问规则或使用 FileSystemAuditRule 类创建审核规则时,请使用 FileSystemRights 枚举。

此枚举包含多个精细的系统权限值,以及这些细化值的组合的多个值。 使用组合值(如 FullControlReadWrite)更容易,而不是单独指定每个组件值。

CreateDirectoriesCreateFiles 权限需要 Synchronize 权限。 如果在创建文件或目录时未显式设置 Synchronize 值,则会自动设置该值。

适用于