FileInfo.GetAccessControl 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 haalt u een FileSecurity object op dat de ACL-vermeldingen (Access Control List) inkapselt voor het bestand dat wordt beschreven door het huidige FileInfo object.
Overloads
| Name | Description |
|---|---|
| GetAccessControl(AccessControlSections) |
Hiermee wordt een FileSecurity object opgehaald dat het opgegeven type ACL-vermeldingen (Access Control List) inkapselt voor het bestand dat wordt beschreven door het huidige FileInfo object. |
| GetAccessControl() |
Hiermee haalt u een FileSecurity object op dat de ACL-vermeldingen (Access Control List) inkapselt voor het bestand dat wordt beschreven door het huidige FileInfo object. |
GetAccessControl(AccessControlSections)
Hiermee wordt een FileSecurity object opgehaald dat het opgegeven type ACL-vermeldingen (Access Control List) inkapselt voor het bestand dat wordt beschreven door het huidige 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
Parameters
- includeSections
- AccessControlSections
Een van de AccessControlSections waarden die aangeeft welke groep vermeldingen voor toegangsbeheer moeten worden opgehaald.
Retouren
Een FileSecurity object dat de toegangsbeheerregels voor het huidige bestand inkapselt.
Uitzonderingen
Er is een I/O-fout opgetreden tijdens het openen van het bestand.
Het huidige systeemaccount heeft geen beheerdersbevoegdheden.
Kan het bestand niet vinden.
Deze bewerking wordt niet ondersteund op het huidige platform.
– of –
De beller heeft niet de vereiste machtiging.
Opmerkingen
Gebruik de GetAccessControl methode om de ACL-vermeldingen (Access Control List) voor het huidige bestand op te halen.
Een ACL beschrijft personen en groepen die al dan niet rechten hebben voor specifieke acties op het opgegeven bestand. Zie Instructies voor het toevoegen of verwijderen van vermeldingen in toegangsbeheerlijsten voor meer informatie.
Van toepassing op
GetAccessControl()
Hiermee haalt u een FileSecurity object op dat de ACL-vermeldingen (Access Control List) inkapselt voor het bestand dat wordt beschreven door het huidige 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
Retouren
Een FileSecurity object dat de toegangsbeheerregels voor het huidige bestand inkapselt.
Uitzonderingen
Er is een I/O-fout opgetreden tijdens het openen van het bestand.
Het huidige systeemaccount heeft geen beheerdersbevoegdheden.
Kan het bestand niet vinden.
Deze bewerking wordt niet ondersteund op het huidige platform.
– of –
De beller heeft niet de vereiste machtiging.
Voorbeelden
In het volgende codevoorbeeld worden de GetAccessControl methode en de SetAccessControl methode 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 = "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.
'
Opmerkingen
Gebruik de GetAccessControl methode om de ACL-vermeldingen (Access Control List) voor het huidige bestand op te halen.
Een ACL beschrijft personen en groepen die al dan niet rechten hebben voor specifieke acties op het opgegeven bestand. Zie Instructies voor het toevoegen of verwijderen van vermeldingen in toegangsbeheerlijsten voor meer informatie.