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



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

GetAccessControl メソッドと SetAccessControl メソッドを使用して、アクセス制御リスト (ACL) のエントリをファイルに追加した後、ファイルからエントリを削除するコード例を次に示します。この例を実行するには、有効なユーザー アカウントまたはグループ アカウントを使用する必要があります。
Imports System Imports System.IO Imports System.Security.AccessControl Module FileExample Sub Main() Try Dim fileName As String = "test.xml" Console.WriteLine("Adding access control entry for " & fileName) ' Add the access control entry to the file. AddFileSecurity(fileName, "DomainName\AccountName", _ FileSystemRights.ReadData, AccessControlType.Allow) Console.WriteLine("Removing access control entry from " & fileName) ' Remove the access control entry from the file. RemoveFileSecurity(fileName, "DomainName\AccountName", _ 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) ' Get a FileSecurity object that represents the ' current security settings. Dim fSecurity As FileSecurity = File.GetAccessControl(fileName) ' Add the FileSystemAccessRule to the security settings. Dim accessRule As FileSystemAccessRule = _ New FileSystemAccessRule(account, rights, controlType) fSecurity.AddAccessRule(accessRule) ' Set the new access settings. File.SetAccessControl(fileName, 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) ' Get a FileSecurity object that represents the ' current security settings. Dim fSecurity As FileSecurity = File.GetAccessControl(fileName) ' Add the FileSystemAccessRule to the security settings. fSecurity.RemoveAccessRule(New FileSystemAccessRule(account, _ rights, controlType)) ' Set the new access settings. File.SetAccessControl(fileName, fSecurity) End Sub End Module
using System; using System.IO; using System.Security.AccessControl; namespace FileSystemExample { class FileExample { public static void Main() { try { string fileName = "test.xml"; Console.WriteLine("Adding access control entry for " + fileName); // Add the access control entry to the file. AddFileSecurity(fileName, @"DomainName\AccountName", FileSystemRights.ReadData, AccessControlType.Allow); Console.WriteLine("Removing access control entry from " + fileName); // Remove the access control entry from the file. RemoveFileSecurity(fileName, @"DomainName\AccountName", 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) { // Get a FileSecurity object that represents the // current security settings. FileSecurity fSecurity = File.GetAccessControl(fileName); // Add the FileSystemAccessRule to the security settings. fSecurity.AddAccessRule(new FileSystemAccessRule(account , rights, controlType)); // Set the new access settings. File.SetAccessControl(fileName, 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) { // Get a FileSecurity object that represents the // current security settings. FileSecurity fSecurity = File.GetAccessControl(fileName); // Add the FileSystemAccessRule to the security settings. fSecurity.RemoveAccessRule(new FileSystemAccessRule(account , rights, controlType)); // Set the new access settings. File.SetAccessControl(fileName, fSecurity); } } }
using namespace System; using namespace System::IO; using namespace System::Security::AccessControl; // Adds an ACL entry on the specified file for the specified account. void AddFileSecurity(String^ fileName, String^ account, FileSystemRights rights, AccessControlType controlType) { // Get a FileSecurity object that represents the // current security settings. FileSecurity^ fSecurity = File::GetAccessControl(fileName); // Add the FileSystemAccessRule to the security settings. fSecurity->AddAccessRule(gcnew FileSystemAccessRule (account,rights, controlType)); // Set the new access settings. File::SetAccessControl(fileName, fSecurity); } // Removes an ACL entry on the specified file for the specified account. void RemoveFileSecurity(String^ fileName, String^ account, FileSystemRights rights, AccessControlType controlType) { // Get a FileSecurity object that represents the // current security settings. FileSecurity^ fSecurity = File::GetAccessControl(fileName); // Remove the FileSystemAccessRule to the security settings. fSecurity->RemoveAccessRule(gcnew FileSystemAccessRule (account,rights, controlType)); // Set the new access settings. File::SetAccessControl(fileName, fSecurity); } int main() { try { String^ fileName = "test.xml"; Console::WriteLine("Adding access control entry for " + fileName); // Add the access control entry to the file. AddFileSecurity(fileName, "MYDOMAIN\\MyAccount", FileSystemRights::ReadData, AccessControlType::Allow); Console::WriteLine("Removing access control entry from " + fileName); // Remove the access control entry from the file. RemoveFileSecurity(fileName, "MYDOMAIN\\MyAccount", FileSystemRights::ReadData, AccessControlType::Allow); Console::WriteLine("Done."); } catch (Exception^ ex) { Console::WriteLine(ex->Message); } }


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


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