Directory.SetAccessControl メソッド
アセンブリ: mscorlib (mscorlib.dll 内)

Dim path As String Dim directorySecurity As DirectorySecurity Directory.SetAccessControl(path, directorySecurity)

例外の種類 | 条件 |
---|---|
ArgumentNullException | directorySecurity パラメータが null 参照 (Visual Basic では Nothing) です。 |
DirectoryNotFoundException | |
ArgumentException | |
UnauthorizedAccessException | |
PlatformNotSupportedException | 現在のオペレーティング システムは Microsoft Windows 2000 以降ではありません。 |

SetAccessControl メソッドは、非継承 ACL リストを表すファイルにアクセス制御リスト (ACL) エントリを適用します。
![]() |
---|
ディレクトリの既存の ACL が、directorySecurity パラメータで指定された ACL に置き換えられます。新しいユーザーのアクセス許可を追加するには、GetAccessControl メソッドを使用して既存の ACL を取得し、これを変更します。 |
-
ACL は、指定したファイルまたはディレクトリ上での特定のアクションに対して権限を持つ (または持たない) 個人およびグループ、またはそのいずれかを示します。詳細については、アクセス制御リスト (ACL: Access Control List) 技術の概要、方法 : アクセス制御リスト エントリを追加または削除する の各トピックを参照してください。
SetAccessControl メソッドでは、作成後に変更された DirectorySecurity オブジェクトのみが保持されます。DirectorySecurity オブジェクトが変更されていない場合は、ファイルに保持されません。したがって、ファイルから DirectorySecurity オブジェクトを取得して、同じオブジェクトを別のファイルに適用することはできません。
ファイルからファイルに ACL 情報をコピーするには、次の操作を実行します。
-
GetAccessControl メソッドを使用して、DirectorySecurity オブジェクトをソース ファイルから取得します。
-
コピー元となる DirectorySecurity オブジェクトの GetSecurityDescriptorBinaryForm メソッドまたは GetSecurityDescriptorSddlForm メソッドを使用して、ACL 情報を取得します。
-
SetSecurityDescriptorBinaryForm メソッドまたは SetSecurityDescriptorSddlForm メソッドを使用して、手順 3 で取得した情報をコピー先の DirectorySecurity オブジェクトにコピーします。
-
SetAccessControl メソッドを使用して、コピー先の DirectorySecurity オブジェクトをコピー先のファイルに設定します。
-
NTFS 環境では、ユーザーが親フォルダに対して ListDirectory 権限を持っている場合、ReadAttributes および ReadExtendedAttributes がそのユーザーに付与されます。ReadAttributes および ReadExtendedAttributes を拒否するには、親ディレクトリで ListDirectory を拒否します。

GetAccessControl メソッドおよび SetAccessControl メソッドを使用して、ディレクトリにアクセス制御リスト (ACL) エントリを追加し、次にディレクトリから 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); } }


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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Weblioに収録されているすべての辞書からDirectory.SetAccessControl メソッドを検索する場合は、下記のリンクをクリックしてください。

- Directory.SetAccessControl メソッドのページへのリンク