FileSystemSecurity.RemoveAuditRule(FileSystemAuditRule) FileSystemSecurity.RemoveAuditRule(FileSystemAuditRule) FileSystemSecurity.RemoveAuditRule(FileSystemAuditRule) FileSystemSecurity.RemoveAuditRule(FileSystemAuditRule) Method

定義

從目前的檔案或目錄中,移除所有相符的允許或拒絕稽核規則。Removes all matching allow or deny audit rules from the current file or directory.

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

參數

rule
FileSystemAuditRule FileSystemAuditRule FileSystemAuditRule FileSystemAuditRule

FileSystemAuditRule 物件,表示要從檔案或目錄中移除的稽核規則。A FileSystemAuditRule object that represents an audit rule to remove from a file or directory.

傳回

true 表示已移除稽核規則;否則為 falsetrue if the audit rule was removed; otherwise, false

例外狀況

範例

下列程式碼範例會使用AddAuditRule方法, 將審核規則新增至檔案, 並RemoveAuditRule使用方法從檔案中移除 audit 規則。The following code example uses the AddAuditRule method to add an audit rule to a file and uses the RemoveAuditRule method to remove the audit rule from the file. 您必須提供有效的使用者或群組帳戶,才能執行這個範例。You must supply a valid user or group account to run this example.

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.
                AddFileAuditRule(FileName, @"MYDOMAIN\MyAccount", FileSystemRights.ReadData, AuditFlags.Failure);

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

                // Remove the access control entry from the file.
                RemoveFileAuditRule(FileName, @"MYDOMAIN\MyAccount", FileSystemRights.ReadData, AuditFlags.Failure);

                Console.WriteLine("Done.");
            }
            catch (IOException e)
            {
                Console.WriteLine("Unable to open the file: " + e.Message);
            }
            catch (PrivilegeNotHeldException e)
            {
                Console.WriteLine("The current account does not have the correct privileges: " + e.Message);
            }

            Console.ReadLine();
        }

        // Adds an ACL entry on the specified file for the specified account.
        public static void AddFileAuditRule(string FileName, string Account, FileSystemRights Rights, AuditFlags AuditRule)
        {


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

            // Add the FileSystemAuditRule to the security settings. 
            fSecurity.AddAuditRule(new FileSystemAuditRule(Account,
                                                            Rights,
                                                            AuditRule));

            // Set the new access settings.
            File.SetAccessControl(FileName, fSecurity);

        }

        // Removes an ACL entry on the specified file for the specified account.
        public static void RemoveFileAuditRule(string FileName, string Account, FileSystemRights Rights, AuditFlags AuditRule)
        {

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

            // Add the FileSystemAuditRule to the security settings. 
            fSecurity.RemoveAuditRule(new FileSystemAuditRule(Account,
                                                            Rights,
                                                            AuditRule));

            // 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.
            AddFileAuditRule(FileName, "MYDOMAIN\MyAccount", FileSystemRights.ReadData, AuditFlags.Failure)

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

            ' Remove the access control entry from the file.
            RemoveFileAuditRule(FileName, "MYDOMAIN\MyAccount", FileSystemRights.ReadData, AuditFlags.Failure)

            Console.WriteLine("Done.")
        Catch e As IOException
            Console.WriteLine("Unable to open the file: " & e.Message)
        Catch e As PrivilegeNotHeldException
            Console.WriteLine("The current account does not have the correct privileges: " & e.Message)
        End Try

        Console.ReadLine()

    End Sub


    ' Adds an ACL entry on the specified file for the specified account.
    Sub AddFileAuditRule(ByVal FileName As String, ByVal Account As String, ByVal Rights As FileSystemRights, ByVal AuditRule As AuditFlags)


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

        ' Add the FileSystemAuditRule to the security settings. 
        fSecurity.AddAuditRule(New FileSystemAuditRule(Account, Rights, AuditRule))

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

    End Sub


    ' Removes an ACL entry on the specified file for the specified account.
    Sub RemoveFileAuditRule(ByVal FileName As String, ByVal Account As String, ByVal Rights As FileSystemRights, ByVal AuditRule As AuditFlags)

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

        ' Add the FileSystemAuditRule to the security settings. 
        fSecurity.RemoveAuditRule(New FileSystemAuditRule(Account, Rights, AuditRule))

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

    End Sub
End Module

備註

方法會從Allow目前DenyFileSystemSecurity物件中移除所有相符的 audit 規則或所有相符的 audit 規則。 RemoveAuditRuleThe RemoveAuditRule method removes either all matching Deny audit rules or all matching Allow audit rules from the current FileSystemSecurity object. 例如, 您可以使用這個方法, 藉由傳遞Deny Failure使用值、值和使用者帳戶所FileSystemAuditRule Deny建立的物件, 來移除使用者的所有審核規則。For example, you can use this method to remove all Deny audit rules for a user by passing a FileSystemAuditRule object created using the Deny value, the Failure value, and a user account. 當您這麼做時, RemoveAuditRule方法會移除任何Failure指定值或Success值的拒絕規則。When you do this, the RemoveAuditRule method removes any deny rules that specify the Failure value or the Success value.

適用於