FileSystemSecurity.RemoveAccessRule(FileSystemAccessRule) メソッド

定義

現在のファイルまたはディレクトリから、一致するすべての許可または拒否アクセス制御リスト (ACL) のアクセス許可を削除します。

public:
 bool RemoveAccessRule(System::Security::AccessControl::FileSystemAccessRule ^ rule);
public bool RemoveAccessRule(System.Security.AccessControl.FileSystemAccessRule rule);
override this.RemoveAccessRule : System.Security.AccessControl.FileSystemAccessRule -> bool
Public Function RemoveAccessRule (rule As FileSystemAccessRule) As Boolean

パラメーター

rule
FileSystemAccessRule

ファイルまたはディレクトリから削除するアクセス制御リスト (ACL) アクセス許可を表す FileSystemAccessRule オブジェクト。

返品

true アクセス規則が削除された場合。それ以外の場合は false

例外

rule パラメーターはnull

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

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)
        {
            FileInfo fileInfo = new(fileName);
            FileSecurity fSecurity = fileInfo.GetAccessControl();

            // Add the FileSystemAccessRule to the security settings.
            fSecurity.AddAccessRule(new FileSystemAccessRule(account,
                rights, controlType));

            // Set the new access settings.
            fileInfo.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)
        {
            FileInfo fileInfo = new(fileName);
            FileSecurity fSecurity = fileInfo.GetAccessControl();

            // Remove the FileSystemAccessRule from the security settings.
            fSecurity.RemoveAccessRule(new FileSystemAccessRule(account,
                rights, controlType));

            // Set the new access settings.
            fileInfo.SetAccessControl(fSecurity);
        }
    }
}
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)

        Dim fileInfo As New FileInfo(fileName)
        Dim fSecurity As FileSecurity = fileInfo.GetAccessControl()

        ' Add the FileSystemAccessRule to the security settings. 
        Dim accessRule As New FileSystemAccessRule(account, rights, controlType)

        fSecurity.AddAccessRule(accessRule)

        ' Set the new access settings.
        fileInfo.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)

        Dim fileInfo As New FileInfo(fileName)
        Dim fSecurity As FileSecurity = fileInfo.GetAccessControl()

        ' Remove the FileSystemAccessRule from the security settings. 
        fSecurity.RemoveAccessRule(New FileSystemAccessRule(account,
            rights, controlType))

        ' Set the new access settings.
        fileInfo.SetAccessControl(fSecurity)

    End Sub
End Module

注釈

RemoveAccessRuleメソッドは、現在のDeny オブジェクトから、一致するすべてのAllowアクセス規則または一致するすべてのFileSystemSecurityアクセス規則を削除します。 たとえば、このメソッドを使用すると、Deny値、FileSystemAccessRule値、およびユーザー アカウントを使用して作成されたDeny オブジェクトを渡すことによって、ユーザーのすべてのReadアクセス規則を削除できます。 これを行うと、 RemoveAccessRule メソッドは、 Read 値または Write 値を指定する拒否規則をすべて削除します。

次の.NET実装に依存するメソッドを使用して、ファイルに ACL 情報を追加または取得します。

.NET 実装 ルールの追加 ルールを取得する
.NET FileSystemAclExtensions.SetAccessControl(FileInfo, FileSecurity) FileSystemAclExtensions.GetAccessControl(FileInfo)
.NET Framework FileInfo.SetAccessControl(FileSecurity) FileInfo.GetAccessControl()

Synchronize フラグを設定せずにアクセス規則を追加すると、Synchronize フラグが規則に自動的に追加されます。 後で Synchronize フラグを指定せずに規則を削除すると、フラグは自動的に削除されます。

適用対象