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?