FileInfo.GetAccessControl Metod
Definition
Viktigt
En del information gäller för förhandsversionen av en produkt och kan komma att ändras avsevärt innan produkten blir allmänt tillgänglig. Microsoft lämnar inga garantier, uttryckliga eller underförstådda, avseende informationen som visas här.
Hämtar ett FileSecurity objekt som kapslar in ACL-poster (Access Control List) för filen som beskrivs av det aktuella FileInfo objektet.
Överlagringar
| Name | Description |
|---|---|
| GetAccessControl(AccessControlSections) |
Hämtar ett FileSecurity objekt som kapslar in den angivna typen av åtkomstkontrollistaposter (ACL) för filen som beskrivs av det aktuella FileInfo objektet. |
| GetAccessControl() |
Hämtar ett FileSecurity objekt som kapslar in ACL-poster (Access Control List) för filen som beskrivs av det aktuella FileInfo objektet. |
GetAccessControl(AccessControlSections)
Hämtar ett FileSecurity objekt som kapslar in den angivna typen av åtkomstkontrollistaposter (ACL) för filen som beskrivs av det aktuella FileInfo objektet.
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
Parametrar
- includeSections
- AccessControlSections
Ett av de AccessControlSections värden som anger vilken grupp av åtkomstkontrollposter som ska hämtas.
Returer
Ett FileSecurity objekt som kapslar in åtkomstkontrollreglerna för den aktuella filen.
Undantag
Ett I/O-fel uppstod när filen öppnades.
Det aktuella systemkontot har inte administratörsbehörighet.
Det gick inte att hitta filen.
Den här åtgärden stöds inte på den aktuella plattformen.
-eller-
Anroparen har inte den behörighet som krävs.
Kommentarer
GetAccessControl Använd metoden för att hämta poster i åtkomstkontrollistan (ACL) för den aktuella filen.
En ACL beskriver individer och grupper som har, eller inte har, rättigheter till specifika åtgärder i den angivna filen. Mer information finns i Så här lägger du till eller tar bort poster för åtkomstkontrollistan.
Gäller för
GetAccessControl()
Hämtar ett FileSecurity objekt som kapslar in ACL-poster (Access Control List) för filen som beskrivs av det aktuella FileInfo objektet.
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
Returer
Ett FileSecurity objekt som kapslar in åtkomstkontrollreglerna för den aktuella filen.
Undantag
Ett I/O-fel uppstod när filen öppnades.
Det aktuella systemkontot har inte administratörsbehörighet.
Det gick inte att hitta filen.
Den här åtgärden stöds inte på den aktuella plattformen.
-eller-
Anroparen har inte den behörighet som krävs.
Exempel
I följande kodexempel används GetAccessControl metoden och SetAccessControl metoden för att lägga till och sedan ta bort en post i åtkomstkontrollistan (ACL) från en fil. Du måste ange ett giltigt användar- eller gruppkonto för att kunna köra det här exemplet.
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.
'
Kommentarer
GetAccessControl Använd metoden för att hämta poster i åtkomstkontrollistan (ACL) för den aktuella filen.
En ACL beskriver individer och grupper som har, eller inte har, rättigheter till specifika åtgärder i den angivna filen. Mer information finns i Så här lägger du till eller tar bort poster för åtkomstkontrollistan.