Home:ALL Converter>AddAccessRule extremly slow on large folders

AddAccessRule extremly slow on large folders

Ask Time:2018-06-08T22:12:05         Author:Nils

Json Formatter

I have some code, which i had to refactor, which got extremly slow after refactoring.

This is what I use now:

DirectoryInfo dInfo = new DirectoryInfo(vPath);
DirectorySecurity dSecurity = dInfo.GetAccessControl();
dSecurity.AddAccessRule(new FileSystemAccessRule(GroupName,
                             FileSystemRights.Read | FileSystemRights.Traverse, 
                             InheritanceFlags.None, 
                             PropagationFlags.None, 
                             AccessControlType.Allow));

dInfo.SetAccessControl(dSecurity);

And this is the previous code, which ran a lot faster:

// Add a new access control entry (ACE) object (objAce) so that
// the NDSuser has Full Control permissions on NTFS file system files.

objAce1 = new AccessControlEntryClass();
objAce1.Trustee = (objSIdHex).ToString();

objAce1.AccessMask = Maske; //(int)1180159;

objAce1.AceType = (int)ActiveDs.ADS_ACETYPE_ENUM.ADS_ACETYPE_ACCESS_ALLOWED;
objAce1.AceFlags = (int)ActiveDs.ADS_ACEFLAG_ENUM.ADS_ACEFLAG_INHERIT_ACE
                 | (int)ActiveDs.ADS_ACEFLAG_ENUM.ADS_ACEFLAG_INHERIT_ONLY_ACE 
                 | 1;

objDAcl.AddAce(objAce1);

I have no clue why this takes so long, especially since I am trying to set rights to only this folder (no inheritance / no propagation) - but on large folder structures it takes minutes!

Author:Nils,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/50762568/addaccessrule-extremly-slow-on-large-folders
yy