FileSystemAccessRule Classe

Definizione

Rappresenta un'astrazione di una voce di controllo di accesso (ACE) che definisce una regola di accesso per un file o una directory. La classe non può essere ereditata.

public ref class FileSystemAccessRule sealed : System::Security::AccessControl::AccessRule
public sealed class FileSystemAccessRule : System.Security.AccessControl.AccessRule
[System.Security.SecurityCritical]
public sealed class FileSystemAccessRule : System.Security.AccessControl.AccessRule
type FileSystemAccessRule = class
    inherit AccessRule
[<System.Security.SecurityCritical>]
type FileSystemAccessRule = class
    inherit AccessRule
Public NotInheritable Class FileSystemAccessRule
Inherits AccessRule
Ereditarietà
FileSystemAccessRule
Attributi

Esempio

Nell'esempio di codice seguente viene usata la FileSecurity classe per aggiungere e quindi rimuovere una voce di controllo di accesso (ACE) da un file. È necessario specificare un utente valido o un account di gruppo per eseguire questo esempio.

using namespace System;
using namespace System::IO;
using namespace System::Security::AccessControl;

// Adds an ACL entry on the specified file for the specified account.

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(gcnew 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.

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(gcnew FileSystemAccessRule
                                      (account,rights, controlType));

    // Set the new access settings.
    File::SetAccessControl(fileName, fSecurity);
}

int main()
{
    try
    {
        String^ fileName = "test.xml";

        Console::WriteLine("Adding access control entry for " + fileName);

        // Add the access control entry to the file.
        AddFileSecurity(fileName, "MYDOMAIN\\MyAccount", 
            FileSystemRights::ReadData, AccessControlType::Allow);

        Console::WriteLine("Removing access control entry from " + fileName);

        // Remove the access control entry from the file.
        RemoveFileSecurity(fileName, "MYDOMAIN\\MyAccount", 
            FileSystemRights::ReadData, AccessControlType::Allow);

        Console::WriteLine("Done.");
    }
    catch (Exception^ ex)
    {
        Console::WriteLine(ex->Message);
    }
}
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

Commenti

La FileSystemAccessRule classe rappresenta un'astrazione di una voce di controllo di accesso sottostante (ACE) che specifica un account utente, il tipo di accesso da fornire (lettura, scrittura e così via) e se consentire o negare tale diritto. Questa classe può anche specificare il modo in cui le regole di accesso vengono propagate agli oggetti figlio.

Usare la FileSystemAccessRule classe per creare una nuova regola di accesso. È possibile mantenere la regola usando la FileSecurity classe o DirectorySecurity .

Costruttori

FileSystemAccessRule(IdentityReference, FileSystemRights, AccessControlType)

Inizializza una nuova istanza della classe FileSystemAccessRule utilizzando un riferimento a un account utente, un valore che specifica il tipo di operazione associata alla regola di accesso e un valore che specifica se consentire o meno l'esecuzione dell'operazione.

FileSystemAccessRule(IdentityReference, FileSystemRights, InheritanceFlags, PropagationFlags, AccessControlType)

Inizializza una nuova istanza della classe FileSystemAccessRule utilizzando un riferimento a un account utente, un valore che specifica il tipo di operazione associata alla regola di accesso, un valore che determina come vengono ereditati i diritti, un valore che determina come vengono propagati i diritti e un valore che specifica se consentire o meno l'esecuzione dell'operazione.

FileSystemAccessRule(String, FileSystemRights, AccessControlType)

Inizializza una nuova istanza della classe FileSystemAccessRule utilizzando il nome di un account utente, un valore che specifica il tipo di operazione associata alla regola di accesso e un valore che indica se consentire o meno l'esecuzione dell'operazione.

FileSystemAccessRule(String, FileSystemRights, InheritanceFlags, PropagationFlags, AccessControlType)

Inizializza una nuova istanza della classe FileSystemAccessRule utilizzando un riferimento a un account utente, un valore che specifica il tipo di operazione associata alla regola di accesso, un valore che determina come vengono ereditati i diritti, un valore che determina come vengono propagati i diritti e un valore che specifica se consentire o meno l'esecuzione dell'operazione.

Proprietà

AccessControlType

Ottiene il valore AccessControlType associato all'oggetto AccessRule.

(Ereditato da AccessRule)
AccessMask

Ottiene la maschera di accesso per questa regola.

(Ereditato da AuthorizationRule)
FileSystemRights

Ottiene i flag FileSystemRights associati all'oggetto FileSystemAccessRule corrente.

IdentityReference

Ottiene l'oggetto IdentityReference a cui si applica questa regola.

(Ereditato da AuthorizationRule)
InheritanceFlags

Ottiene il valore dei flag che determinano come questa regola viene ereditata dagli oggetti figlio.

(Ereditato da AuthorizationRule)
IsInherited

Ottiene un valore che indica se la regola viene impostata in modo esplicito oppure se è ereditata da un oggetto contenitore padre.

(Ereditato da AuthorizationRule)
PropagationFlags

Ottiene il valore dei flag di propagazione, che determinano come l'ereditarietà di questa regola viene propagata agli oggetti figlio. Questa proprietà è significativa solo quando il valore dell'enumerazione InheritanceFlags non è None.

(Ereditato da AuthorizationRule)

Metodi

Equals(Object)

Determina se l'oggetto specificato è uguale all'oggetto corrente.

(Ereditato da Object)
GetHashCode()

Funge da funzione hash predefinita.

(Ereditato da Object)
GetType()

Ottiene l'oggetto Type dell'istanza corrente.

(Ereditato da Object)
MemberwiseClone()

Crea una copia superficiale dell'oggetto Object corrente.

(Ereditato da Object)
ToString()

Restituisce una stringa che rappresenta l'oggetto corrente.

(Ereditato da Object)

Si applica a