Search This Blog

Friday, October 17, 2014

Check Deny Access File C#

C# > System.Security > AccessControlAuthorizationRuleCollection 

AuthorizationRuleCollection 

Represents a collection of AuthorizationRule objects.

Example


Check Deny Access File C#

public bool CheckDenyAccessFile(string filePath)
{
       FileSecurity fs = File.GetAccessControl(filePath);
       AuthorizationRuleCollection acl = fs.GetAccessRules(true, true, typeof(System.Security.Principal.SecurityIdentifier));
       bool deny = false;
       for (int x = 0; x < acl.Count; x++)
       {
              FileSystemAccessRule currentRule = (FileSystemAccessRule)acl[x];
              AccessControlType accessType = currentRule.AccessControlType;
              if (accessType == AccessControlType.Deny && (currentRule.FileSystemRights & FileSystemRights.ListDirectory) == FileSystemRights.ListDirectory)
                {
                    deny = true;
                    break;
                }
        }
        return deny;
}