简体   繁体   中英

Remove permissions from a windows folder

I have a widows folder on a remote server from which I wish to remove permissions for a specific user. I've tried numerous methods and nothing seems to work.

I get no errors with the following code but the permission remains intact. Am I not using the correct objects or missing some step with these objects? Any assistance would be greatly appreciated.

The dirName is passed as a share, eg \\myserver\\myfolder

private void removePermissions(string dirName, string username)
    {
        string user = System.Environment.UserDomainName + "\\" + username;
        DirectoryInfo dirinfo = new DirectoryInfo(dirName);
        DirectorySecurity dsec = dirinfo.GetAccessControl(AccessControlSections.All);

        AuthorizationRuleCollection rules = dsec.GetAccessRules(true, true, typeof(System.Security.Principal.NTAccount));
        foreach (AccessRule rule in rules)
        {
            if (rule.IdentityReference.Value == user)
            {
                bool value;
                dsec.PurgeAccessRules(rule.IdentityReference);
                dsec.ModifyAccessRule(AccessControlModification.RemoveAll, rule, out value);
                MessageBox.Show("Removed permission from " + dirName + " for " + user);
            }
        }
    }

Once you have created the new ACL you need to apply it to the folder.

Add

dirinfo.SetAccessControl(dsec);

after the loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM