HOW TO:新增或移除存取控制清單項目 (僅限 .NET Framework)How to: Add or remove Access Control List entries (.NET Framework only)

若要在檔案或目錄加入或移除存取控制清單 (ACL) 項目,請從檔案或目錄取得 FileSecurityDirectorySecurity 物件。To add or remove Access Control List (ACL) entries to or from a file or directory, get the FileSecurity or DirectorySecurity object from the file or directory. 修改物件,然後將其套回至檔案或目錄。Modify the object, and then apply it back to the file or directory.

在檔案加入或移除 ACL 項目Add or remove an ACL entry from a file

  1. 呼叫 File.GetAccessControl 方法來取得 FileSecurity 物件,其中包含檔案的目前 ACL 項目。Call the File.GetAccessControl method to get a FileSecurity object that contains the current ACL entries of a file.

  2. FileSecurity 步驟 1 中傳回的物件加入或移除 ACL 項目。Add or remove ACL entries from the FileSecurity object returned from step 1.

  3. 若要套用變更,請將 FileSecurity 物件傳遞至 File.SetAccessControl 方法。To apply the changes, pass the FileSecurity object to the File.SetAccessControl method.

在目錄加入或移除 ACL 項目Add or remove an ACL entry from a directory

  1. 呼叫 Directory.GetAccessControl 方法來取得 DirectorySecurity 物件,其中包含目錄的目前 ACL 項目。Call the Directory.GetAccessControl method to get a DirectorySecurity object that contains the current ACL entries of a directory.

  2. DirectorySecurity 步驟 1 中傳回的物件加入或移除 ACL 項目。Add or remove ACL entries from the DirectorySecurity object returned from step 1.

  3. 若要套用變更,請將 DirectorySecurity 物件傳遞至 Directory.SetAccessControl 方法。To apply the changes, pass the DirectorySecurity object to the Directory.SetAccessControl method.

範例Example

您必須使用有效的使用者或群組帳戶,才能執行這個範例。You must use a valid user or group account to run this example. 此範例使用 File 物件。The example uses a File object. FileInfoDirectoryDirectoryInfo 類別使用相同程序。Use the same procedure for the FileInfo, Directory, and DirectoryInfo classes.

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)
        {


            // Get a FileSecurity object that represents the
            // current security settings.
            FileSecurity fSecurity = File.GetAccessControl(fileName);

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

            // Set the new access settings.
            File.SetAccessControl(fileName, 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)
        {

            // Get a FileSecurity object that represents the
            // current security settings.
            FileSecurity fSecurity = File.GetAccessControl(fileName);

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

            // Set the new access settings.
            File.SetAccessControl(fileName, 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)
  
        ' Get a FileSecurity object that represents the 
        ' current security settings.
        Dim fSecurity As FileSecurity = File.GetAccessControl(fileName)

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

        fSecurity.AddAccessRule(accessRule)

        ' Set the new access settings.
        File.SetAccessControl(fileName, 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)

        ' Get a FileSecurity object that represents the 
        ' current security settings.
        Dim fSecurity As FileSecurity = File.GetAccessControl(fileName)

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

        ' Set the new access settings.
        File.SetAccessControl(fileName, fSecurity)

    End Sub
End Module