Auf Englisch lesen

Freigeben über


Hinzufügen oder Entfernen von Zugriffssteuerungslisteneinträgen

Um ACL-Einträge (Access Control List, Zugriffssteuerungsliste) zu einer Datei oder einem Verzeichnis hinzuzufügen oder daraus zu entfernen, rufen Sie das FileSecurity- oder DirectorySecurity-Objekt aus der Datei oder dem Verzeichnis ab. Bearbeiten Sie das Objekt, und wenden Sie es dann wieder auf die Datei oder das Verzeichnis an.

Aus Datei

  1. Rufen Sie die FileSystemAclExtensions.GetAccessControl(FileInfo)-Methode oder für .NET Framework-Apps die FileInfo.GetAccessControl-Methode auf, um ein FileSecurity-Objekt abzurufen, das die aktuellen ACL-Einträge einer Datei enthält.

  2. Fügen Sie ACL-Einträge des FileSecurity-Objekt, die Sie in Schritt 1 erhalten haben, hinzu oder entfernen Sie sie.

  3. Um die Änderungen anzuwenden, übergeben Sie das FileSecurity-Objekt an die FileSystemAclExtensions.SetAccessControl(FileInfo, FileSecurity)-Methode oder für .NET Framework-Apps an die FileInfo.SetAccessControl-Methode.

Aus einem Verzeichnis

  1. Rufen Sie die FileSystemAclExtensions.GetAccessControl(DirectoryInfo)-Methode oder für .NET Framework-Apps die DirectoryInfo.GetAccessControl-Methode auf, um ein DirectorySecurity-Objekt abzurufen, das die aktuellen ACL-Einträge eines Verzeichnisses enthält.

  2. Fügen Sie ACL-Einträge des DirectorySecurity-Objekt, die Sie in Schritt 1 erhalten haben, hinzu oder entfernen Sie sie.

  3. Um die Änderungen anzuwenden, übergeben Sie das DirectorySecurity-Objekt an die FileSystemAclExtensions.SetAccessControl(DirectoryInfo, DirectorySecurity)-Methode oder für .NET Framework-Apps an die DirectoryInfo.SetAccessControl-Methode.

Beispiel

Sie müssen ein gültiges Benutzer- oder Gruppenkonto angeben, um dieses Beispiel auszuführen. In diesem Beispiel wird ein FileInfo-Objekt verwendet. Gehen Sie für die Klasse DirectoryInfo genauso vor.

C#
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);
        }
}