C# DirectoryInfo SetAccessControl
Description
DirectoryInfo SetAccessControl
Applies access control
list (ACL) entries described by a DirectorySecurity object to the directory
described by the current DirectoryInfo object.
Syntax
DirectoryInfo.SetAccessControl
has the following syntax.
public void SetAccessControl(
DirectorySecurity directorySecurity
)
Parameters
DirectoryInfo.SetAccessControl
has the following parameters.
directorySecurity
- An object that describes an ACL entry to apply to the directory described by the path parameter.
Returns
DirectoryInfo.SetAccessControl
method returns
Example
Applies access control list (ACL) entries described by a DirectorySecurity object to the directory described by the current DirectoryInfo object.
using System;/*from w ww . ja va 2 s .c om*/
using System.IO;
using System.Security.AccessControl;
class DirectoryExample
{
public static void Main()
{
string DirectoryName = "TestDirectory";
DirectoryInfo dInfo = new DirectoryInfo(DirectoryName);
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);
}
}
The code above generates the following result.