logo
CSharp_Prog_Guide

Чтобы добавить или удалить элемент acl из каталога

  1. Вызовите метод GetAccessControl для получения объекта DirectorySecurity, содержащего текущие элементы ACL каталога.

  2. Добавьте или удалите элементы списка управления доступом из объекта DirectorySecurity, возвращенного на шаге 1.

  3. Передайте объект DirectorySecurity методу SetAccessControl, чтобы сохранить изменения.

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.

AddFileSecurity(fileName, @"DomainName\AccountName",

FileSystemRights.ReadData, AccessControlType.Allow);

Console.WriteLine("Removing access control entry from "

+ fileName);

// Remove the access control entry from the file.

RemoveFileSecurity(fileName, @"DomainName\AccountName",

FileSystemRights.ReadData, AccessControlType.Allow);

Console.WriteLine("Done.");

}

catch (Exception e)

{

Console.WriteLine(e);

}

}

Пример

------

// Adds an ACL entry on the specified file for the specified account.

public static void AddFileSecurity(string fileName, string account,

FileSystemRights rights, AccessControlType controlType)

{

// Get a FileSecurity object that represents the

// current security settings.

FileSecurity fSecurity = File.GetAccessControl(fileName);

// Add the FileSystemAccessRule to the security settings.

fSecurity.AddAccessRule(new FileSystemAccessRule(account,

rights, controlType));

// Set the new access settings.

File.SetAccessControl(fileName, fSecurity);

}

// Removes an ACL entry on the specified file for the specified account.

public static void RemoveFileSecurity(string fileName, string account,

FileSystemRights rights, AccessControlType controlType)

{

// Get a FileSecurity object that represents the

// current security settings.

FileSecurity fSecurity = File.GetAccessControl(fileName);

// Add the FileSystemAccessRule to the security settings.

fSecurity.RemoveAccessRule(new FileSystemAccessRule(account,

rights, controlType));

// Set the new access settings.

File.SetAccessControl(fileName, fSecurity);

}

}

}

Compiling the Code

You must supply a valid user or group account to run this example. This example uses a File object; however, the same procedure is used for the FileInfo, Directory, and DirectoryInfo classes.

------