DirectoryInfo.SetAccessControl(DirectorySecurity) Yöntem
Tanım
Önemli
Bazı bilgiler ürünün ön sürümüyle ilgilidir ve sürüm öncesinde önemli değişiklikler yapılmış olabilir. Burada verilen bilgilerle ilgili olarak Microsoft açık veya zımni hiçbir garanti vermez.
Bir nesne tarafından DirectorySecurity açıklanan erişim denetimi listesi (ACL) girdilerini geçerli DirectoryInfo nesne tarafından açıklanan dizine uygular.
public:
void SetAccessControl(System::Security::AccessControl::DirectorySecurity ^ directorySecurity);
public void SetAccessControl(System.Security.AccessControl.DirectorySecurity directorySecurity);
member this.SetAccessControl : System.Security.AccessControl.DirectorySecurity -> unit
Public Sub SetAccessControl (directorySecurity As DirectorySecurity)
Parametreler
- directorySecurity
- DirectorySecurity
Geçerli DirectoryInfo nesne tarafından açıklanan dizine uygulanacak bir ACL girdisini açıklayan nesne.
Özel durumlar
directorySecurity parametresi null'dir.
Dosya bulunamadı veya değiştirilemedi.
Geçerli işlemin dosyayı açmaya erişimi yok.
Örnekler
Aşağıdaki örnek, dizinden GetAccessControl erişim denetimi listesi (ACL) girdisi eklemek ve kaldırmak için ve SetAccessControl yöntemlerini kullanır.
using System;
using System.IO;
using System.Security.AccessControl;
namespace FileSystemExample
{
class DirectoryExample
{
public static void Main()
{
try
{
string DirectoryName = "TestDirectory";
Console.WriteLine("Adding access control entry for " + DirectoryName);
// Add the access control entry to the directory.
AddDirectorySecurity(DirectoryName, @"MYDOMAIN\MyAccount", FileSystemRights.ReadData, AccessControlType.Allow);
Console.WriteLine("Removing access control entry from " + DirectoryName);
// Remove the access control entry from the directory.
RemoveDirectorySecurity(DirectoryName, @"MYDOMAIN\MyAccount", FileSystemRights.ReadData, AccessControlType.Allow);
Console.WriteLine("Done.");
}
catch (Exception e)
{
Console.WriteLine(e);
}
Console.ReadLine();
}
// Adds an ACL entry on the specified directory for the specified account.
public static void AddDirectorySecurity(
string DirectoryName,
string Account,
FileSystemRights Rights,
AccessControlType ControlType
)
{
// Create a new DirectoryInfo object.
DirectoryInfo dInfo = new(DirectoryName);
// Get a DirectorySecurity object that represents the
// current security settings.
DirectorySecurity dSecurity = dInfo.GetAccessControl();
// Add the FileSystemAccessRule to the security settings.
dSecurity.AddAccessRule(new FileSystemAccessRule(Account,
Rights,
ControlType));
// Set the new access settings.
dInfo.SetAccessControl(dSecurity);
}
// Removes an ACL entry on the specified directory for the specified account.
public static void RemoveDirectorySecurity(
string DirectoryName,
string Account,
FileSystemRights Rights,
AccessControlType ControlType
)
{
// Create a new DirectoryInfo object.
DirectoryInfo dInfo = new(DirectoryName);
// Get a DirectorySecurity object that represents the
// current security settings.
DirectorySecurity dSecurity = dInfo.GetAccessControl();
// Add the FileSystemAccessRule to the security settings.
dSecurity.RemoveAccessRule(new FileSystemAccessRule(Account,
Rights,
ControlType));
// Set the new access settings.
dInfo.SetAccessControl(dSecurity);
}
}
}
open System
open System.IO
open System.Security.AccessControl
// Adds an ACL entry on the specified directory for the specified account.
let addDirectorySecurity fileName (account: string) rights controlType =
// Create a new DirectoryInfo object.
let dInfo = DirectoryInfo fileName
// Get a DirectorySecurity object that represents the
// current security settings.
let dSecurity = dInfo.GetAccessControl()
// Add the FileSystemAccessRule to the security settings.
dSecurity.AddAccessRule(FileSystemAccessRule(account, rights, controlType))
// Set the new access settings.
dInfo.SetAccessControl dSecurity
// Removes an ACL entry on the specified directory for the specified account.
let removeDirectorySecurity fileName (account: string) rights controlType =
// Create a new DirectoryInfo object.
let dInfo = DirectoryInfo fileName
// Get a DirectorySecurity object that represents the
// current security settings.
let dSecurity = dInfo.GetAccessControl()
// Add the FileSystemAccessRule to the security settings.
dSecurity.RemoveAccessRule(FileSystemAccessRule(account, rights, controlType)) |> ignore
// Set the new access settings.
dInfo.SetAccessControl dSecurity
try
let DirectoryName = "TestDirectory"
printfn $"Adding access control entry for {DirectoryName}"
// Add the access control entry to the directory.
addDirectorySecurity DirectoryName @"MYDOMAIN\MyAccount" FileSystemRights.ReadData AccessControlType.Allow
printfn $"Removing access control entry from {DirectoryName}"
// Remove the access control entry from the directory.
removeDirectorySecurity DirectoryName @"MYDOMAIN\MyAccount" FileSystemRights.ReadData AccessControlType.Allow
printfn "Done."
with e ->
printfn $"{e}"
Imports System.IO
Imports System.Security.AccessControl
Module DirectoryExample
Sub Main()
Try
Dim DirectoryName As String = "TestDirectory"
Console.WriteLine("Adding access control entry for " + DirectoryName)
' Add the access control entry to the directory.
AddDirectorySecurity(DirectoryName, "MYDOMAIN\MyAccount", FileSystemRights.ReadData, AccessControlType.Allow)
Console.WriteLine("Removing access control entry from " + DirectoryName)
' Remove the access control entry from the directory.
RemoveDirectorySecurity(DirectoryName, "MYDOMAIN\MyAccount", FileSystemRights.ReadData, AccessControlType.Allow)
Console.WriteLine("Done.")
Catch e As Exception
Console.WriteLine(e)
End Try
Console.ReadLine()
End Sub
' Adds an ACL entry on the specified directory for the specified account.
Sub AddDirectorySecurity(ByVal FileName As String, ByVal Account As String, ByVal Rights As FileSystemRights, ByVal ControlType As AccessControlType)
' Create a new DirectoryInfoobject.
Dim dInfo As New DirectoryInfo(FileName)
' Get a DirectorySecurity object that represents the
' current security settings.
Dim dSecurity As DirectorySecurity = dInfo.GetAccessControl()
' Add the FileSystemAccessRule to the security settings.
dSecurity.AddAccessRule(New FileSystemAccessRule(Account, Rights, ControlType))
' Set the new access settings.
dInfo.SetAccessControl(dSecurity)
End Sub
' Removes an ACL entry on the specified directory for the specified account.
Sub RemoveDirectorySecurity(ByVal FileName As String, ByVal Account As String, ByVal Rights As FileSystemRights, ByVal ControlType As AccessControlType)
' Create a new DirectoryInfo object.
Dim dInfo As New DirectoryInfo(FileName)
' Get a DirectorySecurity object that represents the
' current security settings.
Dim dSecurity As DirectorySecurity = dInfo.GetAccessControl()
' Add the FileSystemAccessRule to the security settings.
dSecurity.RemoveAccessRule(New FileSystemAccessRule(Account, Rights, ControlType))
' Set the new access settings.
dInfo.SetAccessControl(dSecurity)
End Sub
End Module
Açıklamalar
Erişim denetimi listesi (ACL), belirli bir dosya veya dizin üzerinde belirli eylemlere yönelik haklara sahip olan veya olmayan kişileri ve grupları açıklar. Daha fazla bilgi için bkz . Nasıl yapılır: Erişim Denetimi Listesi Girdileri Ekleme veya Kaldırma.
yöntemi, SetAccessControl ACL girdilerini, doğası olmayan ACL listesini temsil eden bir dosyaya uygular.
Caution
için directorySecurity belirtilen ACL, dizinin mevcut ACL'sinin yerini alır. Yeni bir kullanıcının izinlerini eklemek için yöntemini kullanarak GetAccessControl mevcut ACL'yi alın ve değiştirin.
SetAccessControl yöntemi yalnızca DirectorySecurity nesne oluşturulduktan sonra değiştirilmiş nesneleri kalıcı hale döndürür. Bir DirectorySecurity nesne değiştirilmediyse, bir dosyada kalıcı olmaz. Bu nedenle, bir dosyadan nesne almak ve aynı nesneyi başka bir DirectorySecurity dosyaya yeniden uygulamak mümkün değildir.
ACL bilgilerini bir dosyadan diğerine kopyalamak için:
GetAccessControl Nesnesini kaynak dosyadan almak DirectorySecurity için yöntemini kullanın.
Hedef dosya için yeni DirectorySecurity bir nesne oluşturun.
GetSecurityDescriptorBinaryForm ACL bilgilerini almak için kaynak GetSecurityDescriptorSddlForm nesnenin veya DirectorySecurity yöntemini kullanın.
SetSecurityDescriptorBinaryForm 3. adımda alınan bilgileri hedef SetSecurityDescriptorSddlForm nesneye kopyalamak için or DirectorySecurity yöntemini kullanın.
yöntemini kullanarak hedef DirectorySecurity nesneyi hedef dosyaya SetAccessControl ayarlayın.