2

I am checking if I can access a share folder or not.

This is my code:

var readAllow = false;
var readDeny = false;
var accessControlList = Directory.GetAccessControl(path);
if (accessControlList == null)
    return false;
var accessRules = accessControlList.GetAccessRules(true, true, typeof(System.Security.Principal.SecurityIdentifier));
if (accessRules == null)
    return false;

foreach (FileSystemAccessRule rule in accessRules)
{
    if ((FileSystemRights.Read & rule.FileSystemRights) != FileSystemRights.Read) continue;

    if (rule.AccessControlType == AccessControlType.Allow)
        readAllow = true;
    else if (rule.AccessControlType == AccessControlType.Deny)
        readDeny = true;
}

return readAllow && !readDeny;

My problem is that first time after I restart the PC, Directory.GetAccessControl(path) takes about 8s.

But if I execute it again, it is very fast (<1s).

Why is GetAccessControl function very slow at first time?

How can fix this problem?

GSerg
  • 76,472
  • 17
  • 159
  • 346
D T
  • 3,522
  • 7
  • 45
  • 89
  • Related: [Setting ACE slow for folder with many files](https://stackoverflow.com/q/10174585/7444103). -- [How do we change permissions on a share as fast as Explorer does it?](https://devblogs.microsoft.com/oldnewthing/20151007-00/?p=91421) – Jimi May 08 '19 at 09:12

0 Answers0