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



SetAccessControl メソッドは、非継承 ACL リストを表す現在のファイルにアクセス制御リスト (ACL) エントリを適用します。
ファイルに ACL エントリを追加したり、ファイルから ACL エントリを削除したりする場合は、常に SetAccessControl メソッドを使用します。
![]() |
---|
ファイルの既存の ACL が、fileSecurity パラメータで指定された 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 オブジェクトをコピー先のファイルに設定します。

GetAccessControl メソッドおよび SetAccessControl メソッドを使用して、ファイルに ACL エントリを追加し、次にファイルから 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, "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 e As Exception Console.WriteLine(e) End Try Console.ReadLine() 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) ' Create a new FileInfo object. Dim fInfo As New FileInfo(FileName) ' Get a FileSecurity object that represents the ' current security settings. Dim fSecurity As FileSecurity = fInfo.GetAccessControl() ' Add the FileSystemAccessRule to the security settings. fSecurity.AddAccessRule(New FileSystemAccessRule(Account, Rights, ControlType)) ' Set the new access settings. fInfo.SetAccessControl(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) ' Create a new FileInfo object. Dim fInfo As New FileInfo(FileName) ' Get a FileSecurity object that represents the ' current security settings. Dim fSecurity As FileSecurity = fInfo.GetAccessControl() ' Add the FileSystemAccessRule to the security settings. fSecurity.RemoveAccessRule(New FileSystemAccessRule(Account, Rights, ControlType)) ' Set the new access settings. fInfo.SetAccessControl(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, @"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 e) { Console.WriteLine(e); } Console.ReadLine(); } // Adds an ACL entry on the specified file for the specified account. public static void AddFileSecurity(string FileName, string Account, FileSystemRights Rights, AccessControlType ControlType) { // Create a new FileInfo object. FileInfo fInfo = new FileInfo(FileName); // Get a FileSecurity object that represents the // current security settings. FileSecurity fSecurity = fInfo.GetAccessControl(); // Add the FileSystemAccessRule to the security settings. fSecurity.AddAccessRule(new FileSystemAccessRule(Account , Rights, ControlType)); // Set the new access settings. fInfo.SetAccessControl(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) { // Create a new FileInfo object. FileInfo fInfo = new FileInfo(FileName); // Get a FileSecurity object that represents the // current security settings. FileSecurity fSecurity = fInfo.GetAccessControl(); // Add the FileSystemAccessRule to the security settings. fSecurity.RemoveAccessRule(new FileSystemAccessRule(Account , Rights, ControlType)); // Set the new access settings. fInfo.SetAccessControl(fSecurity); } } }
#using <System.Security.dll> using namespace System; using namespace System::IO; using namespace System::Security::AccessControl; using namespace System::Security::Principal; // Adds an ACL entry on the specified file for the specified account. void AddFileSecurity(String^ fileName, String^ account, FileSystemRights^ rights, AccessControlType^ controlType) { // Create a new FileInfo object. FileInfo^ fInfo = gcnew FileInfo(fileName); // Get a FileSecurity object that represents the // current security settings. FileSecurity^ fSecurity = fInfo->GetAccessControl(); // Add the FileSystemAccessRule to the security settings. fSecurity->AddAccessRule(gcnew FileSystemAccessRule(account, *rights, *controlType)); // Set the new access settings. fInfo->SetAccessControl(fSecurity); } // Removes an ACL entry on the specified file for the specified account. void RemoveFileSecurity(String^ fileName, String^ account, FileSystemRights^ rights, AccessControlType^ controlType) { // Create a new FileInfo object. FileInfo^ fInfo = gcnew FileInfo(fileName); // Get a FileSecurity object that represents the // current security settings. FileSecurity^ fSecurity = fInfo->GetAccessControl(); // Remove the FileSystemAccessRule from the security settings. fSecurity->RemoveAccessRule(gcnew FileSystemAccessRule(account, *rights, *controlType)); // Set the new access settings. fInfo->SetAccessControl(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); } Console::ReadLine(); }


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に収録されているすべての辞書からFileInfo.SetAccessControl メソッドを検索する場合は、下記のリンクをクリックしてください。

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