DirectorySecurity 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
表示目錄的存取控制和稽核安全性。 此類別無法獲得繼承。
public ref class DirectorySecurity sealed : System::Security::AccessControl::FileSystemSecurity
public sealed class DirectorySecurity : System.Security.AccessControl.FileSystemSecurity
[System.Security.SecurityCritical]
public sealed class DirectorySecurity : System.Security.AccessControl.FileSystemSecurity
type DirectorySecurity = class
inherit FileSystemSecurity
[<System.Security.SecurityCritical>]
type DirectorySecurity = class
inherit FileSystemSecurity
Public NotInheritable Class DirectorySecurity
Inherits FileSystemSecurity
- 繼承
-
DirectorySecurity
- 屬性
範例
下列程式代碼範例會 DirectorySecurity 使用 類別,從目錄中新增和移除訪問控制清單 (ACL) 專案。 您必須提供有效的使用者或群組帳戶,才能執行這個範例。
using namespace System;
using namespace System::IO;
using namespace System::Security::AccessControl;
// Adds an ACL entry on the specified directory for the
// specified account.
void AddDirectorySecurity(String^ directoryName, String^ account,
FileSystemRights rights, AccessControlType controlType)
{
// Create a new DirectoryInfo object.
DirectoryInfo^ dInfo = gcnew DirectoryInfo(directoryName);
// Get a DirectorySecurity object that represents the
// current security settings.
DirectorySecurity^ dSecurity = dInfo->GetAccessControl();
// Add the FileSystemAccessRule to the security settings.
dSecurity->AddAccessRule( gcnew FileSystemAccessRule(account,
rights, controlType));
// Set the new access settings.
dInfo->SetAccessControl(dSecurity);
}
// Removes an ACL entry on the specified directory for the
// specified account.
void RemoveDirectorySecurity(String^ directoryName, String^ account,
FileSystemRights rights, AccessControlType controlType)
{
// Create a new DirectoryInfo object.
DirectoryInfo^ dInfo = gcnew DirectoryInfo(directoryName);
// Get a DirectorySecurity object that represents the
// current security settings.
DirectorySecurity^ dSecurity = dInfo->GetAccessControl();
// Add the FileSystemAccessRule to the security settings.
dSecurity->RemoveAccessRule(gcnew FileSystemAccessRule(account,
rights, controlType));
// Set the new access settings.
dInfo->SetAccessControl(dSecurity);
}
int main()
{
String^ directoryName = "TestDirectory";
String^ accountName = "MYDOMAIN\\MyAccount";
if (!Directory::Exists(directoryName))
{
Console::WriteLine("The directory {0} could not be found.",
directoryName);
return 0;
}
try
{
Console::WriteLine("Adding access control entry for {0}",
directoryName);
// Add the access control entry to the directory.
AddDirectorySecurity(directoryName, accountName,
FileSystemRights::ReadData, AccessControlType::Allow);
Console::WriteLine("Removing access control entry from {0}",
directoryName);
// Remove the access control entry from the directory.
RemoveDirectorySecurity(directoryName, accountName,
FileSystemRights::ReadData, AccessControlType::Allow);
Console::WriteLine("Done.");
}
catch (UnauthorizedAccessException^)
{
Console::WriteLine("You are not authorised to carry" +
" out this procedure.");
}
catch (System::Security::Principal::
IdentityNotMappedException^)
{
Console::WriteLine("The account {0} could not be found.", accountName);
}
}
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);
}
}
}
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
備註
類別 DirectorySecurity 會指定系統目錄的訪問許可權,以及如何稽核存取嘗試。 這個類別會將存取權和稽核許可權表示為一組規則。 每個存取規則都是由 FileSystemAccessRule 物件表示,而每個稽核規則則以 FileSystemAuditRule 物件表示。
類別 DirectorySecurity 是基礎 Windows 檔案安全性系統的抽象概念。 在此系統中,每個目錄都有選擇性訪問控制清單 (DACL) ,可控制目錄的存取權,以及系統訪問控制清單 (SACL) ,指定稽核的訪問控制嘗試。 FileSystemAccessRule和 FileSystemAuditRule 類別是訪問控制專案的抽象概念, (ACE) 組成 DACL 和 SCL。
類別 DirectorySecurity 會隱藏 DACL 和 SCL 的許多詳細數據;您不需要擔心 ACE 排序或 Null DACLS。
FileSecurity使用 類別來擷取、新增或變更代表檔案 DACL 和 SACL 的存取規則。
下表列出可用來存取和維護目錄安全性的方法。
建構函式
DirectorySecurity() |
初始化 DirectorySecurity 類別的新執行個體。 |
DirectorySecurity(String, AccessControlSections) |
使用 DirectorySecurity 列舉型別 (Enumeration) 之指定的值,從指定的目錄初始化 AccessControlSections 類別的新執行個體。 |
屬性
方法
擴充方法
CreateDirectory(DirectorySecurity, String) |
建立並傳回目錄,確保其會以指定的目錄安全性建立。 如果目錄已經存在,則會傳回現有的目錄。 |