FileInfo.SetAccessControl(FileSecurity) 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.
Tillämpar poster i åtkomstkontrollistan (ACL) som beskrivs av ett FileSecurity objekt på filen som beskrivs av det aktuella FileInfo objektet.
public:
void SetAccessControl(System::Security::AccessControl::FileSecurity ^ fileSecurity);
public void SetAccessControl(System.Security.AccessControl.FileSecurity fileSecurity);
member this.SetAccessControl : System.Security.AccessControl.FileSecurity -> unit
Public Sub SetAccessControl (fileSecurity As FileSecurity)
Parametrar
- fileSecurity
- FileSecurity
Ett FileSecurity objekt som beskriver en åtkomstkontrollistapost (ACL) som ska tillämpas på den aktuella filen.
Undantag
Parametern fileSecurity är null.
Det gick inte att hitta eller ändra filen.
Den aktuella processen har inte åtkomst till att öppna filen.
Exempel
I följande kodexempel används GetAccessControl metoden och SetAccessControl metoden för att lägga till och sedan ta bort en ACL-post 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
Metoden SetAccessControl tillämpar ACL-poster (Access Control List) på den aktuella filen som representerar den icke-arvsbaserade ACL-listan.
Använd metoden SetAccessControl när du behöver lägga till eller ta bort ACL-poster från en fil.
Caution
Den ACL som angetts för parametern fileSecurity ersätter den befintliga ACL:en för filen. Om du vill lägga till behörigheter för en ny användare använder du GetAccessControl metoden för att hämta den befintliga ACL:n, ändrar den och använder SetAccessControl den sedan för att tillämpa den på filen igen.
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.
Metoden SetAccessControl bevarar endast FileSecurity objekt som har ändrats efter att objektet har skapats. Om ett FileSecurity objekt inte har ändrats sparas det inte i en fil. Därför går det inte att hämta ett FileSecurity objekt från en fil och tillämpa samma objekt igen på en annan fil.
Så här kopierar du ACL-information från en fil till en annan:
GetAccessControl Använd metoden för att hämta FileSecurity objektet från källfilen.
Skapa ett nytt FileSecurity objekt för målfilen.
GetSecurityDescriptorBinaryForm Använd källobjektets FileSecurity metod eller GetSecurityDescriptorSddlForm för att hämta ACL-informationen.
SetSecurityDescriptorBinaryForm Använd metoden eller SetSecurityDescriptorSddlForm för att kopiera informationen som hämtades i steg 3 till målobjektetFileSecurity.
Ange målobjektet FileSecurity till målfilen med hjälp av SetAccessControl metoden .