FileInfo.GetAccessControl FileInfo.GetAccessControl FileInfo.GetAccessControl FileInfo.GetAccessControl Method

Definición

Obtiene un objeto FileSecurity que encapsula las entradas de lista de control de acceso (ACL) del archivo descrito por el objeto FileInfo actual.Gets a FileSecurity object that encapsulates the access control list (ACL) entries for the file described by the current FileInfo object.

Sobrecargas

GetAccessControl() GetAccessControl() GetAccessControl() GetAccessControl()

Obtiene un objeto FileSecurity que encapsula las entradas de lista de control de acceso (ACL) del archivo descrito por el objeto FileInfo actual.Gets a FileSecurity object that encapsulates the access control list (ACL) entries for the file described by the current FileInfo object.

GetAccessControl(AccessControlSections) GetAccessControl(AccessControlSections) GetAccessControl(AccessControlSections) GetAccessControl(AccessControlSections)

Obtiene un objeto FileSecurity que encapsula el tipo especificado de entradas de la lista de control de acceso (ACL) para el archivo descrito por el objeto FileInfo actual.Gets a FileSecurity object that encapsulates the specified type of access control list (ACL) entries for the file described by the current FileInfo object.

Comentarios

Utilice los GetAccessControl métodos para recuperar las entradas de la lista de control de acceso (ACL) para el archivo actual.Use the GetAccessControl methods to retrieve the access control list (ACL) entries for the current file.

GetAccessControl() GetAccessControl() GetAccessControl() GetAccessControl()

Obtiene un objeto FileSecurity que encapsula las entradas de lista de control de acceso (ACL) del archivo descrito por el objeto FileInfo actual.Gets a FileSecurity object that encapsulates the access control list (ACL) entries for the file described by the current FileInfo object.

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

Devoluciones

Objeto FileSecurity que encapsula las reglas de control de acceso para el archivo actual.A FileSecurity object that encapsulates the access control rules for the current file.

Excepciones

Se produjo un error de E/S al abrir el archivo.An I/O error occurred while opening the file.

El sistema operativo actual no es Microsoft Windows 2000 o posterior.The current operating system is not Microsoft Windows 2000 or later.

La cuenta del sistema actual no tiene privilegios administrativos.The current system account does not have administrative privileges.

No se encuentra el archivo.The file could not be found.

Esta operación no es compatible con la plataforma actual.This operation is not supported on the current platform.

O bien-or-

El llamador no dispone del permiso requerido.The caller does not have the required permission.

Ejemplos

En el ejemplo de código siguiente GetAccessControl se usa el SetAccessControl método y el método para agregar y después quitar una entrada de la lista de control de acceso (ACL) de un archivo.The following code example uses the GetAccessControl method and the SetAccessControl method to add and then remove an access control list (ACL) entry from a file. Para ejecutar este ejemplo, debe proporcionar una cuenta de usuario o grupo válida.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.
'

Comentarios

Utilice el GetAccessControl método para recuperar las entradas de la lista de control de acceso (ACL) para el archivo actual.Use the GetAccessControl method to retrieve the access control list (ACL) entries for the current file.

Una ACL describe los usuarios o grupos que tienen o no tienen derechos sobre acciones específicas en el archivo especificado.An ACL describes individuals and/or groups who have, or do not have, rights to specific actions on the given file. Para más información, consulte How to: Add or Remove Access Control List Entries (Cómo: Agregar o quitar entradas de la lista de control de acceso).For more information, see How to: Add or Remove Access Control List Entries.

Seguridad

FileIOPermission
Enumeraciones asociadas: NoAccess,ViewAssociated enumerations: NoAccess, View Acción de seguridad: Bajo.Security action: Demand. Para obtener permiso para leer la lista de control de acceso.For permission to read the access control list.

GetAccessControl(AccessControlSections) GetAccessControl(AccessControlSections) GetAccessControl(AccessControlSections) GetAccessControl(AccessControlSections)

Obtiene un objeto FileSecurity que encapsula el tipo especificado de entradas de la lista de control de acceso (ACL) para el archivo descrito por el objeto FileInfo actual.Gets a FileSecurity object that encapsulates the specified type of access control list (ACL) entries for the file described by the current FileInfo object.

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

Parámetros

includeSections
AccessControlSections AccessControlSections AccessControlSections AccessControlSections

Uno de los valores de AccessControlSections que especifica qué grupo de entradas de control de acceso se van a recuperar.One of the AccessControlSections values that specifies which group of access control entries to retrieve.

Devoluciones

Objeto FileSecurity que encapsula las reglas de control de acceso para el archivo actual.A FileSecurity object that encapsulates the access control rules for the current file.

Excepciones

Se produjo un error de E/S al abrir el archivo.An I/O error occurred while opening the file.

El sistema operativo actual no es Microsoft Windows 2000 o posterior.The current operating system is not Microsoft Windows 2000 or later.

La cuenta del sistema actual no tiene privilegios administrativos.The current system account does not have administrative privileges.

No se encuentra el archivo.The file could not be found.

Esta operación no es compatible con la plataforma actual.This operation is not supported on the current platform.

O bien-or-

El llamador no dispone del permiso requerido.The caller does not have the required permission.

Comentarios

Utilice el GetAccessControl método para recuperar las entradas de la lista de control de acceso (ACL) para el archivo actual.Use the GetAccessControl method to retrieve the access control list (ACL) entries for the current file.

Una ACL describe los usuarios o grupos que tienen o no tienen derechos sobre acciones específicas en el archivo especificado.An ACL describes individuals and/or groups who have, or do not have, rights to specific actions on the given file. Para más información, consulte How to: Add or Remove Access Control List Entries (Cómo: Agregar o quitar entradas de la lista de control de acceso).For more information, see How to: Add or Remove Access Control List Entries.

Seguridad

FileIOPermission
Enumeraciones asociadas: NoAccess,ViewAssociated enumerations: NoAccess, View Acción de seguridad: Bajo.Security action: Demand. Para obtener permiso para leer la lista de control de acceso.For permission to read the access control list.

Se aplica a