FileSystemAuditRule Constructors

Definition

Initializes a new instance of the FileSystemAuditRule class.

Overloads

FileSystemAuditRule(IdentityReference, FileSystemRights, AuditFlags)

Initializes a new instance of the FileSystemAuditRule class using a reference to a user account, a value that specifies the type of operation associated with the audit rule, and a value that specifies when to perform auditing.

FileSystemAuditRule(String, FileSystemRights, AuditFlags)

Initializes a new instance of the FileSystemAuditRule class using a user account name, a value that specifies the type of operation associated with the audit rule, and a value that specifies when to perform auditing.

FileSystemAuditRule(IdentityReference, FileSystemRights, InheritanceFlags, PropagationFlags, AuditFlags)

Initializes a new instance of the FileSystemAuditRule class using the name of a reference to a user account, a value that specifies the type of operation associated with the audit rule, a value that determines how rights are inherited, a value that determines how rights are propagated, and a value that specifies when to perform auditing.

FileSystemAuditRule(String, FileSystemRights, InheritanceFlags, PropagationFlags, AuditFlags)

Initializes a new instance of the FileSystemAuditRule class using the name of a user account, a value that specifies the type of operation associated with the audit rule, a value that determines how rights are inherited, a value that determines how rights are propagated, and a value that specifies when to perform auditing.

FileSystemAuditRule(IdentityReference, FileSystemRights, AuditFlags)

Initializes a new instance of the FileSystemAuditRule class using a reference to a user account, a value that specifies the type of operation associated with the audit rule, and a value that specifies when to perform auditing.

C#
public FileSystemAuditRule(System.Security.Principal.IdentityReference identity, System.Security.AccessControl.FileSystemRights fileSystemRights, System.Security.AccessControl.AuditFlags flags);

Parameters

identity
IdentityReference

An IdentityReference object that encapsulates a reference to a user account.

fileSystemRights
FileSystemRights

One of the FileSystemRights values that specifies the type of operation associated with the audit rule.

flags
AuditFlags

One of the AuditFlags values that specifies when to perform auditing.

Exceptions

The identity parameter is not an IdentityReference object.

The identity parameter is null.

An incorrect enumeration was passed to the flags parameter.

-or-

The None value was passed to the flags parameter.

Remarks

Use this constructor to create an audit rule that you can persist using the FileSecurity or DirectorySecurity class. Audit rules determine when and how actions performed on system objects, such as files and folders, are logged.

Applies to

.NET 10 a ďalšie verzie
Produkt Verzie
.NET Core 1.0, Core 1.1, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5

FileSystemAuditRule(String, FileSystemRights, AuditFlags)

Initializes a new instance of the FileSystemAuditRule class using a user account name, a value that specifies the type of operation associated with the audit rule, and a value that specifies when to perform auditing.

C#
public FileSystemAuditRule(string identity, System.Security.AccessControl.FileSystemRights fileSystemRights, System.Security.AccessControl.AuditFlags flags);

Parameters

identity
String

The name of a user account.

fileSystemRights
FileSystemRights

One of the FileSystemRights values that specifies the type of operation associated with the audit rule.

flags
AuditFlags

One of the AuditFlags values that specifies when to perform auditing.

Exceptions

An incorrect enumeration was passed to the flags parameter.

-or-

The None value was passed to the flags parameter.

Examples

The following code example uses the FileSystemAuditRule class to add and then remove an audit rule from a file. You must supply a valid user or group account to run this example.

C#
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);
        }
    }
}

Remarks

Use this constructor to create an audit rule that you can persist using the FileSecurity or DirectorySecurity class. Audit rules determine when and how actions performed on system objects, such as files and folders, are logged.

The identity parameter must identify a valid account on the current computer or domain. The string takes the following form, where DOMAIN is the name of a valid domain or computer name and account is the name of a valid user account on a domain or computer: DOMAIN\account.

Applies to

.NET 10 a ďalšie verzie
Produkt Verzie
.NET Core 1.0, Core 1.1, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5

FileSystemAuditRule(IdentityReference, FileSystemRights, InheritanceFlags, PropagationFlags, AuditFlags)

Initializes a new instance of the FileSystemAuditRule class using the name of a reference to a user account, a value that specifies the type of operation associated with the audit rule, a value that determines how rights are inherited, a value that determines how rights are propagated, and a value that specifies when to perform auditing.

C#
public FileSystemAuditRule(System.Security.Principal.IdentityReference identity, System.Security.AccessControl.FileSystemRights fileSystemRights, System.Security.AccessControl.InheritanceFlags inheritanceFlags, System.Security.AccessControl.PropagationFlags propagationFlags, System.Security.AccessControl.AuditFlags flags);

Parameters

identity
IdentityReference

An IdentityReference object that encapsulates a reference to a user account.

fileSystemRights
FileSystemRights

One of the FileSystemRights values that specifies the type of operation associated with the audit rule.

inheritanceFlags
InheritanceFlags

One of the InheritanceFlags values that specifies how access masks are propagated to child objects.

propagationFlags
PropagationFlags

One of the PropagationFlags values that specifies how Access Control Entries (ACEs) are propagated to child objects.

flags
AuditFlags

One of the AuditFlags values that specifies when to perform auditing.

Exceptions

The identity parameter is not an IdentityReference object.

The identity parameter is null.

An incorrect enumeration was passed to the flags parameter.

-or-

The None value was passed to the flags parameter.

Remarks

Use this constructor to create an audit rule that you can persist using the FileSecurity or DirectorySecurity class. Audit rules determine when and how actions performed on system objects, such as files and folders, are logged.

Applies to

.NET 10 a ďalšie verzie
Produkt Verzie
.NET Core 1.0, Core 1.1, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5

FileSystemAuditRule(String, FileSystemRights, InheritanceFlags, PropagationFlags, AuditFlags)

Initializes a new instance of the FileSystemAuditRule class using the name of a user account, a value that specifies the type of operation associated with the audit rule, a value that determines how rights are inherited, a value that determines how rights are propagated, and a value that specifies when to perform auditing.

C#
public FileSystemAuditRule(string identity, System.Security.AccessControl.FileSystemRights fileSystemRights, System.Security.AccessControl.InheritanceFlags inheritanceFlags, System.Security.AccessControl.PropagationFlags propagationFlags, System.Security.AccessControl.AuditFlags flags);

Parameters

identity
String

The name of a user account.

fileSystemRights
FileSystemRights

One of the FileSystemRights values that specifies the type of operation associated with the audit rule.

inheritanceFlags
InheritanceFlags

One of the InheritanceFlags values that specifies how access masks are propagated to child objects.

propagationFlags
PropagationFlags

One of the PropagationFlags values that specifies how Access Control Entries (ACEs) are propagated to child objects.

flags
AuditFlags

One of the AuditFlags values that specifies when to perform auditing.

Remarks

Use this constructor to create an audit rule that you can persist using the FileSecurity or DirectorySecurity class. Audit rules determine when and how actions performed on system objects, such as files and folders, are logged.

The identity parameter must identify a valid account on the current computer or domain. The string takes the following form, where DOMAIN is the name of a valid domain or computer name and account is the name of a valid user account on a domain or computer: DOMAIN\account.

Applies to

.NET 10 a ďalšie verzie
Produkt Verzie
.NET Core 1.0, Core 1.1, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5