0

I need check if a specific user (Domain or Local), has mentioned rights (Read / Write) on the given directory.

The method should return true even the User is inheriting the rights from User Group (like Administrators).

This answer works fine but it is limited to Current User only

Community
  • 1
  • 1
knightmare313
  • 95
  • 2
  • 9

1 Answers1

1

Try the bellow function

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

     public static bool CheckWritePermissionOnDir(string path)
        {
            var writeAllow = false;
            var writeDeny = false;
            var accessControlList = Directory.GetAccessControl(path); Control
            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.Write & rule.FileSystemRights) != FileSystemRights.Write)
                    continue;

                if (rule.AccessControlType == AccessControlType.Allow)
                    writeAllow = true;
                else if (rule.AccessControlType == AccessControlType.Deny)
                    writeDeny = true;
            }

            return writeAllow && !writeDeny;
        }
  • but this does not check for a particular user – knightmare313 Mar 24 '15 at 06:56
  • you can use this line SecurityIdentifier users = new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, null); and then add the if inside your foreach statement like (if(rule.IdentityReference == users) before you check the rights – Celimpilo Mncwango Mar 24 '15 at 07:16