Windows apps
Collapse the table of content
Expand the table of content
Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

DirectorySecurity Class

 

Represents the access control and audit security for a directory. This class cannot be inherited.

Namespace:   System.Security.AccessControl
Assembly:  mscorlib (in mscorlib.dll)


public sealed class DirectorySecurity : FileSystemSecurity

NameDescription
System_CAPS_pubmethodDirectorySecurity()

Initializes a new instance of the DirectorySecurity class.

System_CAPS_pubmethodDirectorySecurity(String, AccessControlSections)

Initializes a new instance of the DirectorySecurity class from a specified directory using the specified values of the AccessControlSections enumeration.

NameDescription
System_CAPS_pubpropertyAccessRightType

Gets the enumeration that the FileSystemSecurity class uses to represent access rights.(Inherited from FileSystemSecurity.)

System_CAPS_pubpropertyAccessRuleType

Gets the enumeration that the FileSystemSecurity class uses to represent access rules.(Inherited from FileSystemSecurity.)

System_CAPS_pubpropertyAreAccessRulesCanonical

Gets a Boolean value that specifies whether the access rules associated with this ObjectSecurity object are in canonical order.(Inherited from ObjectSecurity.)

System_CAPS_pubpropertyAreAccessRulesProtected

Gets a Boolean value that specifies whether the Discretionary Access Control List (DACL) associated with this ObjectSecurity object is protected.(Inherited from ObjectSecurity.)

System_CAPS_pubpropertyAreAuditRulesCanonical

Gets a Boolean value that specifies whether the audit rules associated with this ObjectSecurity object are in canonical order.(Inherited from ObjectSecurity.)

System_CAPS_pubpropertyAreAuditRulesProtected

Gets a Boolean value that specifies whether the System Access Control List (SACL) associated with this ObjectSecurity object is protected.(Inherited from ObjectSecurity.)

System_CAPS_pubpropertyAuditRuleType

Gets the type that the FileSystemSecurity class uses to represent audit rules.(Inherited from FileSystemSecurity.)

NameDescription
System_CAPS_pubmethodAccessRuleFactory(IdentityReference, Int32, Boolean, InheritanceFlags, PropagationFlags, AccessControlType)

Initializes a new instance of the FileSystemAccessRule class that represents a new access control rule for the specified user, with the specified access rights, access control, and flags.(Inherited from FileSystemSecurity.)

System_CAPS_pubmethodAddAccessRule(FileSystemAccessRule)

Adds the specified access control list (ACL) permission to the current file or directory.(Inherited from FileSystemSecurity.)

System_CAPS_pubmethodAddAuditRule(FileSystemAuditRule)

Adds the specified audit rule to the current file or directory.(Inherited from FileSystemSecurity.)

System_CAPS_pubmethodAuditRuleFactory(IdentityReference, Int32, Boolean, InheritanceFlags, PropagationFlags, AuditFlags)

Initializes a new instance of the FileSystemAuditRule class representing the specified audit rule for the specified user.(Inherited from FileSystemSecurity.)

System_CAPS_pubmethodEquals(Object)

Determines whether the specified object is equal to the current object.(Inherited from Object.)

System_CAPS_pubmethodGetAccessRules(Boolean, Boolean, Type)

Gets a collection of the access rules associated with the specified security identifier.(Inherited from CommonObjectSecurity.)

System_CAPS_pubmethodGetAuditRules(Boolean, Boolean, Type)

Gets a collection of the audit rules associated with the specified security identifier.(Inherited from CommonObjectSecurity.)

System_CAPS_pubmethodGetGroup(Type)

Gets the primary group associated with the specified owner.(Inherited from ObjectSecurity.)

System_CAPS_pubmethodGetHashCode()

Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethodGetOwner(Type)

Gets the owner associated with the specified primary group.(Inherited from ObjectSecurity.)

System_CAPS_pubmethodGetSecurityDescriptorBinaryForm()

Returns an array of byte values that represents the security descriptor information for this ObjectSecurity object.(Inherited from ObjectSecurity.)

System_CAPS_pubmethodGetSecurityDescriptorSddlForm(AccessControlSections)

Returns the Security Descriptor Definition Language (SDDL) representation of the specified sections of the security descriptor associated with this ObjectSecurity object.(Inherited from ObjectSecurity.)

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_pubmethodModifyAccessRule(AccessControlModification, AccessRule, Boolean)

Applies the specified modification to the Discretionary Access Control List (DACL) associated with this ObjectSecurity object.(Inherited from ObjectSecurity.)

System_CAPS_pubmethodModifyAuditRule(AccessControlModification, AuditRule, Boolean)

Applies the specified modification to the System Access Control List (SACL) associated with this ObjectSecurity object.(Inherited from ObjectSecurity.)

System_CAPS_pubmethodPurgeAccessRules(IdentityReference)

Removes all access rules associated with the specified IdentityReference.(Inherited from ObjectSecurity.)

System_CAPS_pubmethodPurgeAuditRules(IdentityReference)

Removes all audit rules associated with the specified IdentityReference.(Inherited from ObjectSecurity.)

System_CAPS_pubmethodRemoveAccessRule(FileSystemAccessRule)

Removes all matching allow or deny access control list (ACL) permissions from the current file or directory.(Inherited from FileSystemSecurity.)

System_CAPS_pubmethodRemoveAccessRuleAll(FileSystemAccessRule)

Removes all access control list (ACL) permissions for the specified user from the current file or directory.(Inherited from FileSystemSecurity.)

System_CAPS_pubmethodRemoveAccessRuleSpecific(FileSystemAccessRule)

Removes a single matching allow or deny access control list (ACL) permission from the current file or directory.(Inherited from FileSystemSecurity.)

System_CAPS_pubmethodRemoveAuditRule(FileSystemAuditRule)

Removes all matching allow or deny audit rules from the current file or directory.(Inherited from FileSystemSecurity.)

System_CAPS_pubmethodRemoveAuditRuleAll(FileSystemAuditRule)

Removes all audit rules for the specified user from the current file or directory.(Inherited from FileSystemSecurity.)

System_CAPS_pubmethodRemoveAuditRuleSpecific(FileSystemAuditRule)

Removes a single matching allow or deny audit rule from the current file or directory.(Inherited from FileSystemSecurity.)

System_CAPS_pubmethodResetAccessRule(FileSystemAccessRule)

Adds the specified access control list (ACL) permission to the current file or directory and removes all matching ACL permissions.(Inherited from FileSystemSecurity.)

System_CAPS_pubmethodSetAccessRule(FileSystemAccessRule)

Sets the specified access control list (ACL) permission for the current file or directory. (Inherited from FileSystemSecurity.)

System_CAPS_pubmethodSetAccessRuleProtection(Boolean, Boolean)

Sets or removes protection of the access rules associated with this ObjectSecurity object. Protected access rules cannot be modified by parent objects through inheritance.(Inherited from ObjectSecurity.)

System_CAPS_pubmethodSetAuditRule(FileSystemAuditRule)

Sets the specified audit rule for the current file or directory.(Inherited from FileSystemSecurity.)

System_CAPS_pubmethodSetAuditRuleProtection(Boolean, Boolean)

Sets or removes protection of the audit rules associated with this ObjectSecurity object. Protected audit rules cannot be modified by parent objects through inheritance.(Inherited from ObjectSecurity.)

System_CAPS_pubmethodSetGroup(IdentityReference)

Sets the primary group for the security descriptor associated with this ObjectSecurity object.(Inherited from ObjectSecurity.)

System_CAPS_pubmethodSetOwner(IdentityReference)

Sets the owner for the security descriptor associated with this ObjectSecurity object.(Inherited from ObjectSecurity.)

System_CAPS_pubmethodSetSecurityDescriptorBinaryForm(Byte[])

Sets the security descriptor for this ObjectSecurity object from the specified array of byte values.(Inherited from ObjectSecurity.)

System_CAPS_pubmethodSetSecurityDescriptorBinaryForm(Byte[], AccessControlSections)

Sets the specified sections of the security descriptor for this ObjectSecurity object from the specified array of byte values.(Inherited from ObjectSecurity.)

System_CAPS_pubmethodSetSecurityDescriptorSddlForm(String)

Sets the security descriptor for this ObjectSecurity object from the specified Security Descriptor Definition Language (SDDL) string.(Inherited from ObjectSecurity.)

System_CAPS_pubmethodSetSecurityDescriptorSddlForm(String, AccessControlSections)

Sets the specified sections of the security descriptor for this ObjectSecurity object from the specified Security Descriptor Definition Language (SDDL) string.(Inherited from ObjectSecurity.)

System_CAPS_pubmethodToString()

Returns a string that represents the current object.(Inherited from Object.)

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 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.

The following tables lists the methods you can use to access and maintain directory security.

Task

Methods

Add rules

FileSystemSecurity.AddAccessRule

FileSystemSecurity.AddAuditRule

Remove rules

FileSystemSecurity.RemoveAccessRule

FileSystemSecurity.RemoveAuditRule

Retrieve the access control to a directory

Directory.GetAccessControl

DirectoryInfo.GetAccessControl

Persist the access control to a directory

Directory.SetAccessControl

DirectoryInfo.SetAccessControl

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.

using System;
using System.IO;
using System.Security.AccessControl;

namespace FileSystemExample
{
    class DirectoryExample
    {
        public static void Main()
        {
            try
            {
                string DirectoryName = "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 (Exception e)
            {
                Console.WriteLine(e);
            }

            Console.ReadLine();
        }

        // Adds an ACL entry on the specified directory for the specified account.
        public static void AddDirectorySecurity(string FileName, string Account, FileSystemRights Rights, AccessControlType ControlType)
        {
            // Create a new DirectoryInfo object.
            DirectoryInfo dInfo = new DirectoryInfo(FileName);

            // Get a DirectorySecurity object that represents the 
            // current security settings.
            DirectorySecurity dSecurity = dInfo.GetAccessControl();

            // Add the FileSystemAccessRule to the security settings. 
            dSecurity.AddAccessRule(new FileSystemAccessRule(Account,
                                                            Rights,
                                                            ControlType));

            // Set the new access settings.
            dInfo.SetAccessControl(dSecurity);

        }

        // Removes an ACL entry on the specified directory for the specified account.
        public static void RemoveDirectorySecurity(string FileName, string Account, FileSystemRights Rights, AccessControlType ControlType)
        {
            // Create a new DirectoryInfo object.
            DirectoryInfo dInfo = new DirectoryInfo(FileName);

            // Get a DirectorySecurity object that represents the 
            // current security settings.
            DirectorySecurity dSecurity = dInfo.GetAccessControl();

            // Add the FileSystemAccessRule to the security settings. 
            dSecurity.RemoveAccessRule(new FileSystemAccessRule(Account,
                                                            Rights,
                                                            ControlType));

            // Set the new access settings.
            dInfo.SetAccessControl(dSecurity);

        }
    }
}

Universal Windows Platform
Available since 10
.NET Framework
Available since 2.0

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top
Show:
© 2016 Microsoft