共用方式為


如何:新增或移除訪問控制清單專案

若要從檔案或目錄新增或移除訪問控制清單 (ACL) 專案,請從檔案或目錄取得 FileSecurityDirectorySecurity 物件。 修改物件,然後將其套回至檔案或目錄。

從檔案

  1. FileSystemAclExtensions.GetAccessControl(FileInfo) 針對 .NET Framework 應用程式呼叫 或 ,FileInfo.GetAccessControl以取得FileSecurity包含檔案目前 ACL 項目的物件。

  2. FileSecurity 步驟 1 中取得的物件新增或移除 ACL 專案。

  3. 若要套用變更,請將 FileSecurity 對象傳遞至 FileSystemAclExtensions.SetAccessControl(FileInfo, FileSecurity) (或 ,針對 .NET Framework 應用程式) FileInfo.SetAccessControl方法。

從目錄

  1. FileSystemAclExtensions.GetAccessControl(DirectoryInfo) 針對 .NET Framework 應用程式呼叫 或 ,DirectoryInfo.GetAccessControl以取得DirectorySecurity包含目錄目前 ACL 項目的物件。

  2. DirectorySecurity 步驟 1 中取得的物件新增或移除 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