FileInfo.SetAccessControl メソッドとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > FileInfo.SetAccessControl メソッドの意味・解説 

FileInfo.SetAccessControl メソッド

メモ : このメソッドは、.NET Framework version 2.0 で新しく追加されたものです。

FileSecurity オブジェクトが示すアクセス制御リスト (ACL) エントリを、現在の FileInfo オブジェクトが示すファイルに適用します。

名前空間: System.IO
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Sub SetAccessControl ( _
    fileSecurity As FileSecurity _
)
Dim instance As FileInfo
Dim fileSecurity As FileSecurity

instance.SetAccessControl(fileSecurity)
public void SetAccessControl (
    FileSecurity fileSecurity
)
public:
void SetAccessControl (
    FileSecurity^ fileSecurity
)
public void SetAccessControl (
    FileSecurity fileSecurity
)
public function SetAccessControl (
    fileSecurity : FileSecurity
)

パラメータ

fileSecurity

現在のファイルに適用するアクセス制御リスト (ACL) エントリを示す FileSecurity オブジェクト。

例外例外
例外の種類条件

ArgumentNullException

fileSecurity パラメータが null 参照 (Visual Basic では Nothing) です。

SystemException

ファイルが見つからないか、または変更されています。

UnauthorizedAccessException

現在のプロセスには、ファイルを開くために必要なアクセス権がありません。

PlatformNotSupportedException

現在のオペレーティング システムは Microsoft Windows 2000 以降ではありません。

解説解説

SetAccessControl メソッドは、非継承 ACL リストを表す現在のファイルにアクセス制御リスト (ACL) エントリを適用します。

ファイルに ACL エントリを追加したり、ファイルから ACL エントリを削除したりする場合は、常に SetAccessControl メソッドを使用します。

注意に関するメモ注意

ファイルの既存の ACL が、fileSecurity パラメータで指定された ACL と置き換えられます。新しいユーザーのアクセス許可を追加するには、GetAccessControl メソッドを使用して既存の ACL を取得し、これを変更します。次に、SetAccessControl を使用して、変更した ACL をファイルに適用します。

ACL は、指定したファイルに対して特定のアクションを実行する権限を持つ (または持たない) 個人およびグループ、またはそのいずれかを示します。詳細については、アクセス制御リスト (ACL: Access Control List) 技術の概要、方法 : アクセス制御リスト エントリを追加または削除する の各トピックを参照してください。

SetAccessControl メソッドでは、作成後に変更された FileSecurity オブジェクトのみが保持されます。FileSecurity オブジェクトが変更されていない場合は、ファイルに保持されません。したがって、ファイルから FileSecurity オブジェクトを取得して、同じオブジェクトを別のファイルに適用することはできません。

ファイルからファイルに ACL 情報をコピーするには、次の操作を実行します。

  1. GetAccessControl メソッドを使用して、FileSecurity オブジェクトをソース ファイルから取得します。

  2. コピー先のファイルに新しい FileSecurity オブジェクトを作成します。

  3. コピー元となる FileSecurity オブジェクトの GetSecurityDescriptorBinaryForm メソッドまたは GetSecurityDescriptorSddlForm メソッドを使用して、ACL 情報を取得します。

  4. SetSecurityDescriptorBinaryForm メソッドまたは SetSecurityDescriptorSddlForm メソッドを使用して、手順 3 で取得した情報をコピー先の FileSecurity オブジェクトにコピーします。

  5. 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();
}
.NET Framework のセキュリティ.NET Framework のセキュリティ
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



英和和英テキスト翻訳

英語⇒日本語日本語⇒英語

辞書ショートカット

すべての辞書の索引

FileInfo.SetAccessControl メソッドのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



FileInfo.SetAccessControl メソッドのページの著作権

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2026 Microsoft.All rights reserved.

©2026 GRAS Group, Inc.RSS