FileInfo.SetAccessControl(FileSecurity) Metodo

Definizione

Applica le voci dell'elenco di controllo di accesso (ACL) descritte da un oggetto FileSecurity nel file specificato dall'oggetto FileInfo corrente.Applies access control list (ACL) entries described by a FileSecurity object to the file described by the current FileInfo object.

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 di elenco di controllo di accesso (ACL) da applicare al file corrente.A FileSecurity object that describes an access control list (ACL) entry to apply to the current file.

Eccezioni

Il valore del parametro fileSecurity è null.The fileSecurity parameter is null.

Non è stato possibile trovare o modificare il file.The file could not be found or modified.

Il processo corrente non è autorizzato ad accedere per aprire il file.The current process does not have access to open the file.

Esempio

Nell'esempio di codice seguente vengono usati il GetAccessControl metodo e il SetAccessControl metodo per aggiungere e quindi rimuovere una voce ACL da un file.The following code example uses the GetAccessControl method and the SetAccessControl method to add and then remove an ACL entry from a file. È necessario specificare un utente valido o un account di gruppo per eseguire questo esempio.You must supply a valid user or group account to run this example.

#using <System.Security.dll>
using namespace System;
using namespace System::IO;
using namespace System::Security::AccessControl;
using namespace System::Security::Principal;

// Adds an ACL entry on the specified file for the specified account.
static void AddFileSecurity(String^ fileName, String^ account,
                     FileSystemRights^ rights, 
                     AccessControlType^ controlType)
{
    // Create a new FileInfo object.
    FileInfo^ fInfo = gcnew FileInfo(fileName);
    if (!fInfo->Exists)
    {
        fInfo->Create();
    }

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

    // Add the FileSystemAccessRule to the security settings.
    fSecurity->AddAccessRule(gcnew FileSystemAccessRule(account,
        *rights, *controlType));

    // Set the new access settings.
    fInfo->SetAccessControl(fSecurity);
}

// Removes an ACL entry on the specified file for the specified account.
static void RemoveFileSecurity(String^ fileName, String^ account,
                        FileSystemRights^ rights, 
                        AccessControlType^ controlType)
{
    // Create a new FileInfo object.
    FileInfo^ fInfo = gcnew FileInfo(fileName);
    if (!fInfo->Exists)
    {
        fInfo->Create();
    }

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

    // Remove the FileSystemAccessRule from the security settings.
    fSecurity->RemoveAccessRule(gcnew FileSystemAccessRule(account, 
        *rights, *controlType));

    // Set the new access settings.
    fInfo->SetAccessControl(fSecurity);
}

int 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);
    }

}
//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.
//
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 FileInfo(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 FileInfo(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 ereditato.The SetAccessControl method applies access control list (ACL) entries to the current file that represents the noninherited ACL list.

Usare il SetAccessControl Metodo ogni volta che è necessario aggiungere o rimuovere voci ACL da un file.Use the SetAccessControl method whenever you need to add or remove ACL entries from a file.

Attenzione

L'ACL specificato per il fileSecurity parametro sostituisce l'ACL esistente per il file.The ACL specified for the fileSecurity parameter replaces the existing ACL for the file. Per aggiungere autorizzazioni per un nuovo utente, usare il GetAccessControl metodo per ottenere l'ACL esistente, modificarlo e quindi usarlo per SetAccessControl applicarlo di nuovo al file.To add permissions for a new user, use the GetAccessControl method to obtain the existing ACL, modify it, and then use SetAccessControl to apply it back to the file.

Un ACL descrive i singoli utenti e/o i gruppi che dispongono o non dispongono di diritti per azioni specifiche sul file specificato.An ACL describes individuals and/or groups who have, or do not have, rights to specific actions on the given file. Per altre informazioni, vedere Procedura: aggiungere o rimuovere voci dell'elenco di controllo di accesso (ACL).For more information, see How to: Add or Remove Access Control List Entries.

Il SetAccessControl metodo rende permanente solo FileSecurity gli oggetti che sono stati modificati dopo la creazione dell'oggetto.The SetAccessControl method persists only FileSecurity objects that have been modified after object creation. Se un FileSecurity oggetto non è stato modificato, non verrà salvato in modo permanente in un file.If a FileSecurity object has not been modified, it will not be persisted to a file. Non è quindi possibile recuperare un FileSecurity oggetto da un file e riapplicare lo stesso oggetto a un altro file.Therefore, it is not possible to retrieve a FileSecurity object from one file and reapply the same object to another file.

Per copiare le informazioni ACL da un file a un altro:To copy ACL information from one file to another:

  1. Utilizzare il GetAccessControl metodo per recuperare l' FileSecurity oggetto dal file di origine.Use the GetAccessControl method to retrieve the FileSecurity object from the source file.

  2. Creare un nuovo FileSecurity oggetto per il file di destinazione.Create a new FileSecurity object for the destination file.

  3. Utilizzare il GetSecurityDescriptorBinaryForm GetSecurityDescriptorSddlForm metodo o dell'oggetto di origine FileSecurity per recuperare le informazioni ACL.Use the GetSecurityDescriptorBinaryForm or GetSecurityDescriptorSddlForm method of the source FileSecurity object to retrieve the ACL information.

  4. Usare il SetSecurityDescriptorBinaryForm SetSecurityDescriptorSddlForm metodo o per copiare le informazioni recuperate nel passaggio 3 nell'oggetto di destinazione FileSecurity .Use the SetSecurityDescriptorBinaryForm or SetSecurityDescriptorSddlForm method to copy the information retrieved in step 3 to the destination FileSecurity object.

  5. Impostare l' FileSecurity oggetto di destinazione sul file di destinazione utilizzando il SetAccessControl metodo.Set the destination FileSecurity object to the destination file using the SetAccessControl method.

Si applica a