FileInfo.SetAccessControl(FileSecurity) Metodo

Definizione

Applica le voci dell'elenco di controllo di accesso (ACL) descritte da un FileSecurity oggetto al file descritto dall'oggetto corrente FileInfo .

public:
 void SetAccessControl(System::Security::AccessControl::FileSecurity ^ fileSecurity);
public void SetAccessControl(System.Security.AccessControl.FileSecurity fileSecurity);
member this.SetAccessControl : System.Security.AccessControl.FileSecurity -> unit
Public Sub SetAccessControl (fileSecurity As FileSecurity)

Parametri

fileSecurity
FileSecurity

Oggetto FileSecurity che descrive una voce ACL (Access Control List) da applicare al file corrente.

Eccezioni

Il fileSecurity parametro è null.

Impossibile trovare o modificare il file.

Il processo corrente non ha accesso per aprire il file.

Esempio

Nell'esempio di codice seguente viene utilizzato il GetAccessControl metodo e il SetAccessControl metodo per aggiungere e quindi rimuovere una voce ACL da un file. Per eseguire questo esempio, è necessario specificare un account utente o gruppo valido.

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

namespace FileSystemExample
{
    class FileExample
    {
        public static void Main()
        {
            try
            {
                string FileName = "c:/test.xml";

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

                // Add the access control entry to the file.
                // Before compiling this snippet, change MyDomain to your
                // domain name and MyAccessAccount to the name
                // you use to access your domain.
                AddFileSecurity(FileName, @"MyDomain\MyAccessAccount", FileSystemRights.ReadData, AccessControlType.Allow);

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

                // Remove the access control entry from the file.
                // Before compiling this snippet, change MyDomain to your
                // domain name and MyAccessAccount to the name
                // you use to access your domain.
                RemoveFileSecurity(FileName, @"MyDomain\MyAccessAccount", 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
            )
        {
            // Create a new FileInfo object.
            FileInfo fInfo = new(FileName);

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

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

            // Set the new access settings.
            fInfo.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
            )
        {
            // Create a new FileInfo object.
            FileInfo fInfo = new(FileName);

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

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

            // Set the new access settings.
            fInfo.SetAccessControl(fSecurity);
        }
    }
}
//This code produces output similar to the following;
//results may vary based on the computer/file structure/etc.:
//
//Adding access control entry for c:\test.xml
//Removing access control entry from c:\test.xml
//Done.
//
Imports System.IO
Imports System.Security.AccessControl



Module FileExample

    Sub Main()
        Try
            Dim FileName As String = "c:\test.xml"

            Console.WriteLine("Adding access control entry for " & FileName)

            ' Add the access control entry to the file.
            ' Before compiling this snippet, change MyDomain to your 
            ' domain name and MyAccessAccount to the name 
            ' you use to access your domain.
            AddFileSecurity(FileName, "MyDomain\\MyAccessAccount", FileSystemRights.ReadData, AccessControlType.Allow)

            Console.WriteLine("Removing access control entry from " & FileName)

            ' Remove the access control entry from the file.
            ' Before compiling this snippet, change MyDomain to your 
            ' domain name and MyAccessAccount to the name 
            ' you use to access your domain.
            RemoveFileSecurity(FileName, "MyDomain\\MyAccessAccount", 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)
        ' Create a new FileInfo object.
        Dim fInfo As New FileInfo(FileName)

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

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

        ' Set the new access settings.
        fInfo.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)
        ' Create a new FileInfo object.
        Dim fInfo As New FileInfo(FileName)

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

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

        ' Set the new access settings.
        fInfo.SetAccessControl(fSecurity)

    End Sub
End Module
'This code produces output similar to the following; 
'results may vary based on the computer/file structure/etc.:
'
'Adding access control entry for c:\test.xml
'Removing access control entry from c:\test.xml
'Done.
'

Commenti

Il SetAccessControl metodo applica le voci dell'elenco di controllo di accesso (ACL) al file corrente che rappresenta l'elenco ACL non inrito.

Usare il SetAccessControl metodo ogni volta che è necessario aggiungere o rimuovere voci ACL da un file.

Caution

L'ACL specificato per il fileSecurity parametro sostituisce l'ACL esistente per il file. Per aggiungere autorizzazioni per un nuovo utente, usare il GetAccessControl metodo per ottenere l'ACL esistente, modificarlo e quindi usarlo SetAccessControl per applicarlo nuovamente al file.

Un elenco di controllo di accesso descrive singoli utenti e gruppi che dispongono o non dispongono di diritti per azioni specifiche nel file specificato. Per altre informazioni, vedere Procedura: Aggiungere o rimuovere voci dell'elenco di controllo di accesso.

Il SetAccessControl metodo rende persistenti solo FileSecurity gli oggetti modificati dopo la creazione dell'oggetto. Se un FileSecurity oggetto non è stato modificato, non verrà salvato in modo permanente in un file. Pertanto, non è possibile recuperare un FileSecurity oggetto da un file e riapplicare lo stesso oggetto a un altro file.

Per copiare le informazioni ACL da un file a un altro:

  1. Utilizzare il GetAccessControl metodo per recuperare l'oggetto FileSecurity dal file di origine.

  2. Creare un nuovo FileSecurity oggetto per il file di destinazione.

  3. Utilizzare il GetSecurityDescriptorBinaryForm metodo o GetSecurityDescriptorSddlForm dell'oggetto di origine FileSecurity per recuperare le informazioni ACL.

  4. Utilizzare il SetSecurityDescriptorBinaryForm metodo o SetSecurityDescriptorSddlForm per copiare le informazioni recuperate nel passaggio 3 nell'oggetto di destinazione FileSecurity .

  5. Impostare l'oggetto di destinazione sul file di destinazione FileSecurity usando il SetAccessControl metodo .

Si applica a