FileSystemSecurity.RemoveAccessRule(FileSystemAccessRule) FileSystemSecurity.RemoveAccessRule(FileSystemAccessRule) FileSystemSecurity.RemoveAccessRule(FileSystemAccessRule) FileSystemSecurity.RemoveAccessRule(FileSystemAccessRule) Method

定義

從目前的檔案或目錄中,移除所有相符的允許或拒絕存取控制清單 (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 FileSystemAccessRule FileSystemAccessRule FileSystemAccessRule

FileSystemAccessRule 物件,表示要從檔案或目錄中移除的存取控制清單 (ACL) 使用權限。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.

例外狀況

範例

下列程式碼範例會使用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

備註

方法會從Allow目前DenyFileSystemSecurity物件中移除所有相符的存取規則或所有相符的存取規則。 RemoveAccessRuleThe 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 資訊, 請使用GetAccessControlGetAccessControl方法。To retrieve ACL information from a file, use the GetAccessControl or GetAccessControl method. 若要從目錄取出 ACL 資訊, 請使用GetAccessControlGetAccessControl方法。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.

適用於