FileSystemSecurity.AddAuditRule(FileSystemAuditRule) Méthode

Définition

Ajoute la règle d'audit spécifiée au fichier ou répertoire en cours.

public:
 void AddAuditRule(System::Security::AccessControl::FileSystemAuditRule ^ rule);
public void AddAuditRule (System.Security.AccessControl.FileSystemAuditRule rule);
override this.AddAuditRule : System.Security.AccessControl.FileSystemAuditRule -> unit
Public Sub AddAuditRule (rule As FileSystemAuditRule)

Paramètres

rule
FileSystemAuditRule

Objet FileSystemAuditRule qui représente une règle d'audit à ajouter à un fichier ou un répertoire.

Exceptions

Le paramètre rule a la valeur null.

Exemples

L’exemple de code suivant utilise la AddAuditRule méthode pour ajouter une règle d’audit à un fichier et utilise la RemoveAuditRule méthode pour supprimer la règle d’audit du fichier. Vous devez entrer un compte d'utilisateur ou de groupe valide pour exécuter cet exemple.

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

Remarques

La AddAuditRule méthode ajoute une nouvelle règle d’audit à la liste des règles contenues dans un FileSystemSecurity objet.

Si une règle d’audit existe déjà pour la règle spécifiée, la AddAuditRule méthode ajoute toujours la règle.

S’applique à