如何:添加或移除访问控制列表项

若要从文件或目录添加或删除访问控制列表 (ACL) 条目,请从文件或目录获取 FileSecurityDirectorySecurity 对象。 修改对象,然后将其应用回文件或目录。

从文件中

  1. 调用 FileSystemAclExtensions.GetAccessControl(FileInfo)(或者,对于 .NET Framework 应用为 FileInfo.GetAccessControl)方法来获取包含文件的当前 ACL 条目的 FileSecurity 对象。

  2. 从在步骤 1 中获取的 FileSecurity 对象中添加或删除 ACL 条目。

  3. 若要应用更改,请将 FileSecurity 对象传递给 FileSystemAclExtensions.SetAccessControl(FileInfo, FileSecurity)(或者,对于 .NET Framework 应用为 FileInfo.SetAccessControl)方法。

从目录

  1. 调用 FileSystemAclExtensions.GetAccessControl(DirectoryInfo)(或者,对于 .NET Framework 应用为 DirectoryInfo.GetAccessControl)方法来获取包含目录的当前 ACL 条目的 DirectorySecurity 对象。

  2. 从在步骤 1 中获取的 DirectorySecurity 对象中添加或删除 ACL 条目。

  3. 若要应用更改,请将 DirectorySecurity 对象传递给 FileSystemAclExtensions.SetAccessControl(DirectoryInfo, DirectorySecurity)(或者,对于 .NET Framework 应用为 DirectoryInfo.SetAccessControl)方法。

示例

你必须指定有效的用户或组帐户以运行此示例。 此示例使用 FileInfo 对象。 对 DirectoryInfo 类使用相同的过程。

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

    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