FileSystemSecurity.RemoveAccessRule(FileSystemAccessRule) メソッド

定義

現在のファイルまたはディレクトリから、アクセス制御リスト (ACL) の一致するすべての許可するアクセス許可、または拒否するアクセス許可を削除します。Removes all matching allow or deny access control list (ACL) permissions from the current file or directory.

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 オブジェクト。A FileSystemAccessRule object that represents an access control list (ACL) permission to remove from a file or directory.

戻り値

アクセス規則が削除された場合は true。それ以外の場合は falsetrue if the access rule was removed; otherwise, false.

例外

rule パラメーターが null です。The rule parameter is null.

次のコード例では、FileSecurity クラスを使用して、ファイルからアクセス制御リスト (ACL) エントリを追加してから削除します。The following code example uses the FileSecurity class to add and then remove an access control list (ACL) entry from a file. この例を実行するには、有効なユーザーまたはグループ アカウントを指定する必要があります。You must supply a valid user or group account to run this example.

using namespace System;
using namespace System::IO;
using namespace System::Security::AccessControl;

// Adds an ACL entry on the specified file for the specified account.

void AddFileSecurity(String^ fileName, String^ account, 
                        FileSystemRights rights, AccessControlType controlType)
{
    // Get a FileSecurity object that represents the 
    // current security settings.
    FileSecurity^ fSecurity = File::GetAccessControl(fileName);

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

    // Set the new access settings.
    File::SetAccessControl(fileName, fSecurity);
}

// Removes an ACL entry on the specified file for the specified account.

void RemoveFileSecurity(String^ fileName, String^ account, 
                        FileSystemRights rights, AccessControlType controlType)
{

    // Get a FileSecurity object that represents the 
    // current security settings.
    FileSecurity^ fSecurity = File::GetAccessControl(fileName);

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

    // Set the new access settings.
    File::SetAccessControl(fileName, 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->Message);
    }
}

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)
        {

            // Get a FileSecurity object that represents the
            // current security settings.
            FileSecurity fSecurity = File.GetAccessControl(fileName);

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

            // Set the new access settings.
            File.SetAccessControl(fileName, 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)
        {

            // Get a FileSecurity object that represents the
            // current security settings.
            FileSecurity fSecurity = File.GetAccessControl(fileName);

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

            // Set the new access settings.
            File.SetAccessControl(fileName, 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)
  
        ' Get a FileSecurity object that represents the 
        ' current security settings.
        Dim fSecurity As FileSecurity = File.GetAccessControl(fileName)

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

        fSecurity.AddAccessRule(accessRule)

        ' Set the new access settings.
        File.SetAccessControl(fileName, 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)

        ' Get a FileSecurity object that represents the 
        ' current security settings.
        Dim fSecurity As FileSecurity = File.GetAccessControl(fileName)

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

        ' Set the new access settings.
        File.SetAccessControl(fileName, fSecurity)

    End Sub
End Module

注釈

RemoveAccessRule メソッドは、現在の FileSystemSecurity オブジェクトから、一致するすべての Deny アクセスルールまたはすべての一致する Allow アクセスルールのいずれかを削除します。The RemoveAccessRule method removes either all matching Deny access rules or all matching Allow access rules from the current FileSystemSecurity object. たとえば、このメソッドを使用すると、Deny 値、Read 値、およびユーザーアカウントを使用して作成された FileSystemAccessRule オブジェクトを渡すことによって、ユーザーのすべての Deny アクセス規則を削除できます。For example, you can use this method to remove all Deny access rules for a user by passing a FileSystemAccessRule object created using the Deny value, the Read value, and a user account. これを行うと、RemoveAccessRule メソッドによって、Read 値または Write 値を指定するすべての拒否規則が削除されます。When you do this, the RemoveAccessRule method removes any deny rules that specify the Read value or the Write value.

新規または変更されたアクセス制御リスト (ACL) の情報をファイルに保存するには、SetAccessControl または SetAccessControl 方法を使用します。To persist new or changed access control list (ACL) information to a file, use the SetAccessControl or SetAccessControl method. 新規または変更された ACL 情報をディレクトリに保持するには、SetAccessControl または SetAccessControl 方法を使用します。To persist new or changed ACL information to a directory, use the SetAccessControl or SetAccessControl method.

ファイルから ACL 情報を取得するには、GetAccessControl または GetAccessControl 方法を使用します。To retrieve ACL information from a file, use the GetAccessControl or GetAccessControl method. ディレクトリから ACL 情報を取得するには、GetAccessControl または GetAccessControl 方法を使用します。To retrieve ACL information from a directory, use the GetAccessControl or GetAccessControl method.

Synchronize フラグを設定せずにアクセス規則を追加すると、Synchronize フラグが規則に自動的に追加されます。When you add an access rule without setting the Synchronize flag, the Synchronize flag will be automatically added to your rule. 後で Synchronize フラグを指定せずに規則を削除した場合、フラグは自動的に削除されます。If you remove the rule later without specifying the Synchronize flag, the flag will automatically be removed.

適用対象