C# Directory GetAccessControl(String)

Description

Directory GetAccessControl(String) Gets a DirectorySecurity object that encapsulates the access control list (ACL) entries for a specified directory.

Syntax

Directory.GetAccessControl(String) has the following syntax.


public static DirectorySecurity GetAccessControl(
  string path
)

Parameters

Directory.GetAccessControl(String) has the following parameters.

  • path - The path to a directory containing a DirectorySecurity object that describes the file's access control list (ACL) information.

Returns

Directory.GetAccessControl(String) method returns An object that encapsulates the access control rules for the file described by the path parameter.

Example

The following example uses the GetAccessControl and the SetAccessControl methods to add an access control list (ACL) entry and then remove an ACL entry from a directory.


/* w  w  w.j  a  v  a 2s .  co  m*/
using System;
using System.IO;
using System.Security.AccessControl;

class DirectoryExample
{
    public static void Main()
    {
        string FileName = "TestDirectory";

        DirectoryInfo dInfo = new DirectoryInfo(FileName);

        DirectorySecurity dSecurity = dInfo.GetAccessControl();

        dSecurity.AddAccessRule(new FileSystemAccessRule(@"MYDOMAIN\MyAccount",
                                                        FileSystemRights.ReadData,
                                                        AccessControlType.Allow));

        dInfo.SetAccessControl(dSecurity);

        dSecurity.RemoveAccessRule(new FileSystemAccessRule(@"MYDOMAIN\MyAccount",
                                                        FileSystemRights.ReadData,
                                                        AccessControlType.Allow));
        dInfo.SetAccessControl(dSecurity);

    }
}




















Home »
  C# Tutorial »
    System.IO »




BinaryReader
BinaryWriter
Directory
DirectoryInfo
DriveInfo
File
FileInfo
FileStream
MemoryStream
Path
StreamReader
StreamWriter
StringReader
StringWriter