DirectorySecurity クラス
アセンブリ: mscorlib (mscorlib.dll 内)
構文
DirectorySecurity クラスは、システム ディレクトリのアクセス権およびアクセス試行の監査方法を指定します。このクラスは、一連の規則としてアクセス権と監査権限を表します。各アクセス規則は FileSystemAccessRule オブジェクトによって表され、各監査規則は FileSystemAuditRule オブジェクトによって表されます。
DirectorySecurity クラスは、基になる Microsoft Windows ファイル セキュリティ システムを抽象化したクラスです。このシステムでは、ディレクトリへのアクセスを制御する随意アクセス制御リスト (DACL: Discretionary Access Control List) と監査対象のアクセス制御試行を指定するシステム アクセス制御リスト (SACL: System Access Control List) を各ディレクトリが持ちます。FileSystemAccessRule クラスと FileSystemAuditRule クラスは、DACL と SACL を構成するアクセス制御エントリ (ACE: Access Control Entry) を抽象化したクラスです。
DirectorySecurity クラスでは、DACL および SACL の詳細の多くは非表示になります。したがって、ACE の順序や null 値を持つ DACL を気にする必要はありません。
ファイルの DACL および SACL を表すアクセス規則を取得、追加、または変更するには、FileSecurity クラスを使用します。
ディレクトリに対する新規または変更済みのアクセス規則や監査規則を保持するには、SetAccessControl メソッドまたは SetAccessControl メソッドを使用します。既存のディレクトリからアクセス規則または監査規則を取得するには、GetAccessControl メソッドまたは GetAccessControl メソッドを使用します。
DirectorySecurity クラスを使用してアクセス制御リスト (ACL) のエントリを追加した後、そのエントリをディレクトリから削除するコード例を次に示します。この例を実行するには、有効なユーザー アカウントまたはグループ アカウントを指定する必要があります。
Imports System 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
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 FileName, string Account, FileSystemRights Rights, AccessControlType ControlType) { // Create a new DirectoryInfo object. DirectoryInfo dInfo = new DirectoryInfo(FileName); // 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 FileName, string Account, FileSystemRights Rights, AccessControlType ControlType) { // Create a new DirectoryInfo object. DirectoryInfo dInfo = new DirectoryInfo(FileName); // 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); } } }
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); } }
System.Security.AccessControl.ObjectSecurity
System.Security.AccessControl.CommonObjectSecurity
System.Security.AccessControl.NativeObjectSecurity
System.Security.AccessControl.FileSystemSecurity
System.Security.AccessControl.DirectorySecurity
プラットフォーム
Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
参照
- DirectorySecurity クラスのページへのリンク