DirectorySecurity Class
Assembly: mscorlib (in mscorlib.dll)
The DirectorySecurity class specifies the access rights for a system directory and how access attempts are audited. This class represents access and audit rights as a set of rules. Each access rule is represented by a FileSystemAccessRule object, while each audit rule is represented by a FileSystemAuditRule object.
The DirectorySecurity class is an abstraction of the underlying Microsoft Windows file security system. In this system, each directory has a discretionary access control list (DACL), which controls access to the directory, and a system access control list (SACL), which specifies the access control attempts that are audited. The FileSystemAccessRule and FileSystemAuditRule classes are abstractions of the access control entries (ACEs) that comprise DACLs and SACLs.
The DirectorySecurity class hides many of the details of DACLs and SACLs; you do not have to worry about ACE ordering or null DACLS.
Use the FileSecurity class to retrieve, add, or change the access rules that represent the DACL and SACL of a file.
To persist new or changed access or audit rules to a directory, use the SetAccessControl or SetAccessControl method. To retrieve access or audit rules from an existing directory, use the GetAccessControl or GetAccessControl method.
The following code example uses the DirectorySecurity class to add and then remove an access control list (ACL) entry from a directory. You must supply a valid user or group account to run this example.
Imports System Imports System.IO Imports System.Security.AccessControl Module DirectoryExample Sub Main() Try Dim DirectoryName As String = "TestDirectory" Console.WriteLine("Adding access control entry for " + DirectoryName) ' Add the access control entry to the directory. AddDirectorySecurity(DirectoryName, "MYDOMAIN\MyAccount", FileSystemRights.ReadData, AccessControlType.Allow) Console.WriteLine("Removing access control entry from " + DirectoryName) ' Remove the access control entry from the directory. RemoveDirectorySecurity(DirectoryName, "MYDOMAIN\MyAccount", FileSystemRights.ReadData, AccessControlType.Allow) Console.WriteLine("Done.") Catch e As Exception Console.WriteLine(e) End Try Console.ReadLine() End Sub ' Adds an ACL entry on the specified directory for the specified account. Sub AddDirectorySecurity(ByVal FileName As String, ByVal Account As String, ByVal Rights As FileSystemRights, ByVal ControlType As AccessControlType) ' Create a new DirectoryInfoobject. Dim dInfo As New DirectoryInfo(FileName) ' Get a DirectorySecurity object that represents the ' current security settings. Dim dSecurity As DirectorySecurity = dInfo.GetAccessControl() ' Add the FileSystemAccessRule to the security settings. dSecurity.AddAccessRule(New FileSystemAccessRule(Account, Rights, ControlType)) ' Set the new access settings. dInfo.SetAccessControl(dSecurity) End Sub ' Removes an ACL entry on the specified directory for the specified account. Sub RemoveDirectorySecurity(ByVal FileName As String, ByVal Account As String, ByVal Rights As FileSystemRights, ByVal ControlType As AccessControlType) ' Create a new DirectoryInfo object. Dim dInfo As New DirectoryInfo(FileName) ' Get a DirectorySecurity object that represents the ' current security settings. Dim dSecurity As DirectorySecurity = dInfo.GetAccessControl() ' Add the FileSystemAccessRule to the security settings. dSecurity.RemoveAccessRule(New FileSystemAccessRule(Account, Rights, ControlType)) ' Set the new access settings. dInfo.SetAccessControl(dSecurity) End Sub End Module
System.Security.AccessControl.ObjectSecurity
System.Security.AccessControl.CommonObjectSecurity
System.Security.AccessControl.NativeObjectSecurity
System.Security.AccessControl.FileSystemSecurity
System.Security.AccessControl.DirectorySecurity
Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.