FileSystemSecurity.AddAccessRule(FileSystemAccessRule) Método
Definição
Importante
Algumas informações dizem respeito a um produto pré-lançado que pode ser substancialmente modificado antes de ser lançado. A Microsoft não faz garantias, de forma expressa ou implícita, em relação à informação aqui apresentada.
Adiciona a permissão especificada da lista de controlo de acesso (ACL) ao ficheiro ou diretório atual.
public:
void AddAccessRule(System::Security::AccessControl::FileSystemAccessRule ^ rule);
public void AddAccessRule(System.Security.AccessControl.FileSystemAccessRule rule);
override this.AddAccessRule : System.Security.AccessControl.FileSystemAccessRule -> unit
Public Sub AddAccessRule (rule As FileSystemAccessRule)
Parâmetros
- rule
- FileSystemAccessRule
Um FileSystemAccessRule objeto que representa uma permissão de lista de controlo de acesso (ACL) para adicionar a um ficheiro ou diretório.
Exceções
O rule parâmetro é null.
Exemplos
O exemplo de código seguinte utiliza a FileSecurity classe para adicionar e depois remover uma entrada de lista de controlo de acesso (ACL) de um ficheiro. Deve fornecer uma conta de utilizador ou grupo válida para executar este exemplo.
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)
{
FileInfo fileInfo = new(fileName);
FileSecurity fSecurity = fileInfo.GetAccessControl();
// Add the FileSystemAccessRule to the security settings.
fSecurity.AddAccessRule(new FileSystemAccessRule(account,
rights, controlType));
// Set the new access settings.
fileInfo.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)
{
FileInfo fileInfo = new(fileName);
FileSecurity fSecurity = fileInfo.GetAccessControl();
// Remove the FileSystemAccessRule from the security settings.
fSecurity.RemoveAccessRule(new FileSystemAccessRule(account,
rights, controlType));
// Set the new access settings.
fileInfo.SetAccessControl(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)
Dim fileInfo As New FileInfo(fileName)
Dim fSecurity As FileSecurity = fileInfo.GetAccessControl()
' Add the FileSystemAccessRule to the security settings.
Dim accessRule As New FileSystemAccessRule(account, rights, controlType)
fSecurity.AddAccessRule(accessRule)
' Set the new access settings.
fileInfo.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)
Dim fileInfo As New FileInfo(fileName)
Dim fSecurity As FileSecurity = fileInfo.GetAccessControl()
' Remove the FileSystemAccessRule from the security settings.
fSecurity.RemoveAccessRule(New FileSystemAccessRule(account,
rights, controlType))
' Set the new access settings.
fileInfo.SetAccessControl(fSecurity)
End Sub
End Module
Observações
O AddAccessRule método adiciona uma nova regra à lista de regras contidas num FileSystemSecurity objeto.
Se já existir uma lista de controlo de acesso (ACL) para a regra especificada, o AddAccessRule método continuará a adicionar a regra, com uma exceção: um FileSystemAccessRule objeto criado usando o Deny valor de enumeração substitui um objeto criado usando o valor de Allow enumeração.
Use os seguintes métodos dependentes da implementação .NET para adicionar ou recuperar informação ACL de um ficheiro:
| Implementação .NET | Adicionar regras | Regras de recuperação |
|---|---|---|
| .NET | FileSystemAclExtensions.SetAccessControl(FileInfo, FileSecurity) | FileSystemAclExtensions.GetAccessControl(FileInfo) |
| .NET Framework | FileInfo.SetAccessControl(FileSecurity) | FileInfo.GetAccessControl() |
Quando adiciona uma regra de acesso sem definir a Synchronize bandeira, a Synchronize bandeira será automaticamente adicionada à sua regra. Se remover a regra mais tarde sem especificar a Synchronize bandeira, a bandeira será automaticamente removida.