本文整理汇总了C#中System.Security.AccessControl.FileSystemAuditRule类的典型用法代码示例。如果您正苦于以下问题:C# FileSystemAuditRule类的具体用法?C# FileSystemAuditRule怎么用?C# FileSystemAuditRule使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
FileSystemAuditRule类属于System.Security.AccessControl命名空间,在下文中一共展示了FileSystemAuditRule类的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
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);
}
}
}