Export (0) Print
Expand All

FileSystemSecurity.RemoveAuditRule Method (FileSystemAuditRule)

Removes all matching allow or deny audit rules from the current file or directory.

Namespace:  System.Security.AccessControl
Assembly:  mscorlib (in mscorlib.dll)

'Declaration
Public Function RemoveAuditRule ( _
	rule As FileSystemAuditRule _
) As Boolean

Parameters

rule
Type: System.Security.AccessControl.FileSystemAuditRule

A FileSystemAuditRule object that represents an audit rule to remove from a file or directory.

Return Value

Type: System.Boolean
true if the audit rule was removed; otherwise, false

ExceptionCondition
ArgumentNullException

The rule parameter is Nothing.

The RemoveAuditRule method removes either all matching Deny audit rules or all matching Allow audit rules from the current FileSystemSecurity object. 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. When you do this, the RemoveAuditRule method removes any deny rules that specify the Failure value or the Success value.

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.

Imports System
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 Exception
            Console.WriteLine(e)
        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

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Show:
© 2015 Microsoft