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セキュリティ
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照


このページでは「.NET Framework クラス ライブラリ リファレンス」からFileInfo.SetAccessControl メソッドを検索した結果を表示しています。
Weblioに収録されているすべての辞書からFileInfo.SetAccessControl メソッドを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からFileInfo.SetAccessControl メソッド を検索

英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

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

   

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



FileInfo.SetAccessControl メソッドのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

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

©2024 GRAS Group, Inc.RSS