英語で読む

次の方法で共有


FileInfo.GetAccessControl メソッド

定義

現在の FileInfo オブジェクトによって記述されたファイルのアクセス制御リスト (ACL) エントリをカプセル化する FileSecurity オブジェクトを取得します。

オーバーロード

GetAccessControl()

現在の FileInfo オブジェクトによって記述されたファイルのアクセス制御リスト (ACL) エントリをカプセル化する FileSecurity オブジェクトを取得します。

GetAccessControl(AccessControlSections)

現在の FileInfo オブジェクトによって記述されたファイルの指定した種類のアクセス制御リスト (ACL) エントリをカプセル化する FileSecurity オブジェクトを取得します。

GetAccessControl()

現在の FileInfo オブジェクトによって記述されたファイルのアクセス制御リスト (ACL) エントリをカプセル化する FileSecurity オブジェクトを取得します。

C#
public System.Security.AccessControl.FileSecurity GetAccessControl();

戻り値

現在のファイルのアクセス制御規則をカプセル化する FileSecurity オブジェクト。

例外

ファイルを開くときに I/O エラーが発生しました。

現在のシステム アカウントには管理者特権がありません。

ファイルが見つかりませんでした。

この操作は、現在のプラットフォームではサポートされていません。

-又は-

呼び出し元に必要なアクセス許可がありません。

次のコード例では、GetAccessControl メソッドと SetAccessControl メソッドを使用して、アクセス制御リスト (ACL) エントリをファイルに追加して削除します。 この例を実行するには、有効なユーザーまたはグループ アカウントを指定する必要があります。

C#
using System;
using System.IO;
using System.Security.AccessControl;

namespace FileSystemExample
{
    class FileExample
    {
        public static void Main()
        {
            try
            {
                string FileName = "c:/test.xml";

                Console.WriteLine("Adding access control entry for " + FileName);

                // Add the access control entry to the file.
                // Before compiling this snippet, change MyDomain to your
                // domain name and MyAccessAccount to the name
                // you use to access your domain.
                AddFileSecurity(FileName, @"MyDomain\MyAccessAccount", FileSystemRights.ReadData, AccessControlType.Allow);

                Console.WriteLine("Removing access control entry from " + FileName);

                // Remove the access control entry from the file.
                // Before compiling this snippet, change MyDomain to your
                // domain name and MyAccessAccount to the name
                // you use to access your domain.
                RemoveFileSecurity(FileName, @"MyDomain\MyAccessAccount", 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
            )
        {
            // Create a new FileInfo object.
            FileInfo fInfo = new(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(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);
        }
    }
}
//This code produces output similar to the following;
//results may vary based on the computer/file structure/etc.:
//
//Adding access control entry for c:\test.xml
//Removing access control entry from c:\test.xml
//Done.
//

注釈

GetAccessControl メソッドを使用して、現在のファイルのアクセス制御リスト (ACL) エントリを取得します。

ACL は、特定のファイルに対する特定のアクションに対する権限を持っている(または持っていない)個人とグループを表します。 詳細については、「方法: アクセス制御リスト エントリ追加または削除する」を参照してください。

適用対象

.NET Framework 4.8.1 およびその他のバージョン
製品 バージョン
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

GetAccessControl(AccessControlSections)

現在の FileInfo オブジェクトによって記述されたファイルの指定した種類のアクセス制御リスト (ACL) エントリをカプセル化する FileSecurity オブジェクトを取得します。

C#
public System.Security.AccessControl.FileSecurity GetAccessControl(System.Security.AccessControl.AccessControlSections includeSections);

パラメーター

includeSections
AccessControlSections

取得するアクセス制御エントリのグループを指定する AccessControlSections 値の 1 つ。

戻り値

現在のファイルのアクセス制御規則をカプセル化する FileSecurity オブジェクト。

例外

ファイルを開くときに I/O エラーが発生しました。

現在のシステム アカウントには管理者特権がありません。

ファイルが見つかりませんでした。

この操作は、現在のプラットフォームではサポートされていません。

-又は-

呼び出し元に必要なアクセス許可がありません。

注釈

GetAccessControl メソッドを使用して、現在のファイルのアクセス制御リスト (ACL) エントリを取得します。

ACL は、特定のファイルに対する特定のアクションに対する権限を持っている(または持っていない)個人とグループを表します。 詳細については、「方法: アクセス制御リスト エントリ追加または削除する」を参照してください。

適用対象

.NET Framework 4.8.1 およびその他のバージョン
製品 バージョン
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1