简体   繁体   English

从Windows文件夹中删除权限

[英]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. 我在远程服务器上有一个widows文件夹,我希望从该文件夹中删除特定用户的权限。 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 dirName作为共享传递,例如\\ 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. 一旦创建了新的ACL,您需要将其应用于文件夹。

Add

dirinfo.SetAccessControl(dsec);

after the loop. 循环后。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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