FileSystemSecurity.RemoveAccessRule(FileSystemAccessRule) Methode
Definitie
Belangrijk
Bepaalde informatie heeft betrekking op een voorlopige productversie die aanzienlijk kan worden gewijzigd voordat deze wordt uitgebracht. Microsoft biedt geen enkele expliciete of impliciete garanties met betrekking tot de informatie die hier wordt verstrekt.
Hiermee verwijdert u alle overeenkomende ACL-machtigingen (Allow or Deny Access Control List) uit het huidige bestand of de huidige map.
public:
bool RemoveAccessRule(System::Security::AccessControl::FileSystemAccessRule ^ rule);
public bool RemoveAccessRule(System.Security.AccessControl.FileSystemAccessRule rule);
override this.RemoveAccessRule : System.Security.AccessControl.FileSystemAccessRule -> bool
Public Function RemoveAccessRule (rule As FileSystemAccessRule) As Boolean
Parameters
- rule
- FileSystemAccessRule
Een FileSystemAccessRule object dat een toegangsbeheerlijstmachtiging (ACL) vertegenwoordigt die uit een bestand of map moet worden verwijderd.
Retouren
true als de toegangsregel is verwijderd; anders, false.
Uitzonderingen
De rule parameter is null.
Voorbeelden
In het volgende codevoorbeeld wordt de FileSecurity klasse gebruikt om een ACL-vermelding (Access Control List) toe te voegen en vervolgens uit een bestand te verwijderen. U moet een geldig gebruikers- of groepsaccount opgeven om dit voorbeeld uit te voeren.
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
Opmerkingen
De RemoveAccessRule methode verwijdert alle overeenkomende toegangsregels of alle overeenkomende AllowDeny toegangsregels uit het huidige FileSystemSecurity object. U kunt deze methode bijvoorbeeld gebruiken om alle Deny toegangsregels voor een gebruiker te verwijderen door een FileSystemAccessRule object dat is gemaakt door te geven met behulp van de Deny waarde, de Read waarde en een gebruikersaccount. Wanneer u dit doet, verwijdert de RemoveAccessRule methode alle regels voor weigeren die de Read waarde of de Write waarde opgeven.
Gebruik de volgende .NET implementatieafhankelijke methoden om ACL-gegevens uit een bestand toe te voegen of op te halen:
| .NET-implementatie | Regels toevoegen | Regels ophalen |
|---|---|---|
| .NET | FileSystemAclExtensions.SetAccessControl(FileInfo, FileSecurity) | FileSystemAclExtensions.GetAccessControl(FileInfo) |
| .NET Framework | FileInfo.SetAccessControl(FileSecurity) | FileInfo.GetAccessControl() |
Wanneer u een toegangsregel toevoegt zonder de Synchronize vlag in te stellen, wordt de Synchronize vlag automatisch toegevoegd aan uw regel. Als u de regel later verwijdert zonder de Synchronize vlag op te geven, wordt de vlag automatisch verwijderd.