DirectoryInfo.GetAccessControl Methode

Definitie

Hiermee haalt u de toegangsbeheerlijstvermeldingen (ACL) op voor de huidige map.

Overloads

Name Description
GetAccessControl()

Hiermee haalt u een DirectorySecurity object op dat de ACL-vermeldingen (Access Control List) inkapselt voor de map die wordt beschreven door het huidige DirectoryInfo object.

GetAccessControl(AccessControlSections)

Hiermee haalt u een DirectorySecurity object op dat het opgegeven type ACL-vermeldingen (Access Control List) inkapselt voor de map die wordt beschreven door het huidige DirectoryInfo object.

Opmerkingen

Gebruik GetAccessControl methoden om de ACL-vermeldingen (Access Control List) voor het huidige bestand op te halen.

Zie Instructies voor het toevoegen of verwijderen van vermeldingen in toegangsbeheerlijsten voor meer informatie.

GetAccessControl()

Hiermee haalt u een DirectorySecurity object op dat de ACL-vermeldingen (Access Control List) inkapselt voor de map die wordt beschreven door het huidige DirectoryInfo object.

public:
 System::Security::AccessControl::DirectorySecurity ^ GetAccessControl();
public System.Security.AccessControl.DirectorySecurity GetAccessControl();
member this.GetAccessControl : unit -> System.Security.AccessControl.DirectorySecurity
Public Function GetAccessControl () As DirectorySecurity

Retouren

Een DirectorySecurity object dat de toegangsbeheerregels voor de map inkapselt.

Uitzonderingen

Kan de map niet vinden of wijzigen.

De map heeft het kenmerk Alleen-lezen.

– of –

Deze bewerking wordt niet ondersteund op het huidige platform.

– of –

De beller heeft niet de vereiste machtiging.

Er is een I/O-fout opgetreden tijdens het openen van de map.

Voorbeelden

In het volgende voorbeeld worden de GetAccessControl en SetAccessControl methoden gebruikt om een ACL-vermelding (Access Control List) toe te voegen en vervolgens uit een map te verwijderen.

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

namespace FileSystemExample
{
    class DirectoryExample
    {
        public static void Main()
        {
            try
            {
                string DirectoryName = "TestDirectory";

                Console.WriteLine("Adding access control entry for " + DirectoryName);

                // Add the access control entry to the directory.
                AddDirectorySecurity(DirectoryName, @"MYDOMAIN\MyAccount", FileSystemRights.ReadData, AccessControlType.Allow);

                Console.WriteLine("Removing access control entry from " + DirectoryName);

                // Remove the access control entry from the directory.
                RemoveDirectorySecurity(DirectoryName, @"MYDOMAIN\MyAccount", FileSystemRights.ReadData, AccessControlType.Allow);

                Console.WriteLine("Done.");
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

            Console.ReadLine();
        }

        // Adds an ACL entry on the specified directory for the specified account.
        public static void AddDirectorySecurity(
            string DirectoryName,
            string Account,
            FileSystemRights Rights,
            AccessControlType ControlType
            )
        {
            // Create a new DirectoryInfo object.
            DirectoryInfo dInfo = new(DirectoryName);

            // Get a DirectorySecurity object that represents the
            // current security settings.
            DirectorySecurity dSecurity = dInfo.GetAccessControl();

            // Add the FileSystemAccessRule to the security settings.
            dSecurity.AddAccessRule(new FileSystemAccessRule(Account,
                                                            Rights,
                                                            ControlType));

            // Set the new access settings.
            dInfo.SetAccessControl(dSecurity);
        }

        // Removes an ACL entry on the specified directory for the specified account.
        public static void RemoveDirectorySecurity(
            string DirectoryName,
            string Account,
            FileSystemRights Rights,
            AccessControlType ControlType
            )
        {
            // Create a new DirectoryInfo object.
            DirectoryInfo dInfo = new(DirectoryName);

            // Get a DirectorySecurity object that represents the
            // current security settings.
            DirectorySecurity dSecurity = dInfo.GetAccessControl();

            // Add the FileSystemAccessRule to the security settings.
            dSecurity.RemoveAccessRule(new FileSystemAccessRule(Account,
                                                            Rights,
                                                            ControlType));

            // Set the new access settings.
            dInfo.SetAccessControl(dSecurity);
        }
    }
}
open System
open System.IO
open System.Security.AccessControl

// Adds an ACL entry on the specified directory for the specified account.
let addDirectorySecurity fileName (account: string) rights controlType =
    // Create a new DirectoryInfo object.
    let dInfo = DirectoryInfo fileName

    // Get a DirectorySecurity object that represents the
    // current security settings.
    let dSecurity = dInfo.GetAccessControl()

    // Add the FileSystemAccessRule to the security settings.
    dSecurity.AddAccessRule(FileSystemAccessRule(account, rights, controlType))

    // Set the new access settings.
    dInfo.SetAccessControl dSecurity

// Removes an ACL entry on the specified directory for the specified account.
let removeDirectorySecurity fileName (account: string) rights controlType =
    // Create a new DirectoryInfo object.
    let dInfo = DirectoryInfo fileName

    // Get a DirectorySecurity object that represents the
    // current security settings.
    let dSecurity = dInfo.GetAccessControl()

    // Add the FileSystemAccessRule to the security settings.
    dSecurity.RemoveAccessRule(FileSystemAccessRule(account, rights, controlType)) |> ignore

    // Set the new access settings.
    dInfo.SetAccessControl dSecurity

try
    let DirectoryName = "TestDirectory"

    printfn $"Adding access control entry for {DirectoryName}"

    // Add the access control entry to the directory.
    addDirectorySecurity DirectoryName @"MYDOMAIN\MyAccount" FileSystemRights.ReadData AccessControlType.Allow

    printfn $"Removing access control entry from {DirectoryName}"

    // Remove the access control entry from the directory.
    removeDirectorySecurity DirectoryName @"MYDOMAIN\MyAccount" FileSystemRights.ReadData AccessControlType.Allow

    printfn "Done."
with e ->
    printfn $"{e}"
Imports System.IO
Imports System.Security.AccessControl



Module DirectoryExample

    Sub Main()
        Try
            Dim DirectoryName As String = "TestDirectory"

            Console.WriteLine("Adding access control entry for " + DirectoryName)

            ' Add the access control entry to the directory.
            AddDirectorySecurity(DirectoryName, "MYDOMAIN\MyAccount", FileSystemRights.ReadData, AccessControlType.Allow)

            Console.WriteLine("Removing access control entry from " + DirectoryName)

            ' Remove the access control entry from the directory.
            RemoveDirectorySecurity(DirectoryName, "MYDOMAIN\MyAccount", FileSystemRights.ReadData, AccessControlType.Allow)

            Console.WriteLine("Done.")
        Catch e As Exception
            Console.WriteLine(e)
        End Try

        Console.ReadLine()

    End Sub


    ' Adds an ACL entry on the specified directory for the specified account.
    Sub AddDirectorySecurity(ByVal FileName As String, ByVal Account As String, ByVal Rights As FileSystemRights, ByVal ControlType As AccessControlType)
        ' Create a new DirectoryInfoobject.
        Dim dInfo As New DirectoryInfo(FileName)

        ' Get a DirectorySecurity object that represents the 
        ' current security settings.
        Dim dSecurity As DirectorySecurity = dInfo.GetAccessControl()

        ' Add the FileSystemAccessRule to the security settings. 
        dSecurity.AddAccessRule(New FileSystemAccessRule(Account, Rights, ControlType))

        ' Set the new access settings.
        dInfo.SetAccessControl(dSecurity)

    End Sub


    ' Removes an ACL entry on the specified directory for the specified account.
    Sub RemoveDirectorySecurity(ByVal FileName As String, ByVal Account As String, ByVal Rights As FileSystemRights, ByVal ControlType As AccessControlType)
        ' Create a new DirectoryInfo object.
        Dim dInfo As New DirectoryInfo(FileName)

        ' Get a DirectorySecurity object that represents the 
        ' current security settings.
        Dim dSecurity As DirectorySecurity = dInfo.GetAccessControl()

        ' Add the FileSystemAccessRule to the security settings. 
        dSecurity.RemoveAccessRule(New FileSystemAccessRule(Account, Rights, ControlType))

        ' Set the new access settings.
        dInfo.SetAccessControl(dSecurity)

    End Sub
End Module

Opmerkingen

Het aanroepen van deze methodeoverbelasting komt overeen met het aanroepen van de GetAccessControl methode overbelasting en het opgeven van de secties voor toegangsbeheer AccessControlSections.Access | AccessControlSections.Owner | AccessControlSections.Group (AccessControlSections.AccessOrAccessControlSections.OwnerOrAccessControlSections.Group in Visual Basic).

Een ACL beschrijft personen en groepen die al dan niet beschikken over specifieke acties voor het opgegeven bestand of de opgegeven map. Zie Instructies voor het toevoegen of verwijderen van vermeldingen in toegangsbeheerlijsten voor meer informatie.

Van toepassing op

GetAccessControl(AccessControlSections)

Hiermee haalt u een DirectorySecurity object op dat het opgegeven type ACL-vermeldingen (Access Control List) inkapselt voor de map die wordt beschreven door het huidige DirectoryInfo object.

public:
 System::Security::AccessControl::DirectorySecurity ^ GetAccessControl(System::Security::AccessControl::AccessControlSections includeSections);
public System.Security.AccessControl.DirectorySecurity GetAccessControl(System.Security.AccessControl.AccessControlSections includeSections);
member this.GetAccessControl : System.Security.AccessControl.AccessControlSections -> System.Security.AccessControl.DirectorySecurity
Public Function GetAccessControl (includeSections As AccessControlSections) As DirectorySecurity

Parameters

includeSections
AccessControlSections

Een van de AccessControlSections waarden die het type toegangsbeheerlijst (ACL) aangeeft dat moet worden ontvangen.

Retouren

Een DirectorySecurity object dat de toegangsbeheerregels inkapselt voor de map die door het huidige DirectoryInfo object wordt beschreven.

Uitzonderingen

Kan de map niet vinden of wijzigen.

Het huidige proces heeft geen toegang om de map te openen.
OR
De map heeft het kenmerk Alleen-lezen.
OR
Deze bewerking wordt niet ondersteund op het huidige platform.
OR
De beller heeft niet de vereiste machtiging.

Er is een I/O-fout opgetreden tijdens het openen van de map.

Opmerkingen

Gebruik de GetAccessControl methode om de ACL-vermeldingen (Access Control List) voor het huidige bestand op te halen.

Een ACL beschrijft personen en groepen die al dan niet beschikken over specifieke acties voor het opgegeven bestand of de opgegeven map. Zie Instructies voor het toevoegen of verwijderen van vermeldingen in toegangsbeheerlijsten voor meer informatie.

Van toepassing op