简体   繁体   English

如何在不进行硬编码的情况下删除文件夹的所有拒绝权限?

[英]How to remove all deny permissions on a folder without hardcoding?

I'm using microsoft's RemoveDirectorySecurity method to remove a folder's permissions.我正在使用微软的RemoveDirectorySecurity方法来删除文件夹的权限。

public static void RemoveDirectorySecurity(string FileName, string Account, FileSystemRights Rights, AccessControlType ControlType)
{
    DirectoryInfo dInfo = new DirectoryInfo(FileName);
    DirectorySecurity dSecurity = dInfo.GetAccessControl();

    dSecurity.RemoveAccessRule(new FileSystemAccessRule(Account,
                                                        Rights,
                                                        ControlType));

    dInfo.SetAccessControl(dSecurity);
}

I hardcoded it using the Account parameter to remove all permissions.我使用Account参数对其进行硬编码以删除所有权限。

private void RemoveAllDenyPermission(string tempDirectory, string userName)
{
    RemoveDirectorySecurity(tempDirectory, "system", FileSystemRights.FullControl, AccessControlType.Allow);
    RemoveDirectorySecurity(tempDirectory, "users", FileSystemRights.FullControl, AccessControlType.Allow);
    RemoveDirectorySecurity(tempDirectory, "Everyone", FileSystemRights.FullControl, AccessControlType.Allow);
    RemoveDirectorySecurity(tempDirectory, "Administrators", FileSystemRights.FullControl, AccessControlType.Allow);            
    RemoveDirectorySecurity(tempDirectory, userName, FileSystemRights.FullControl, AccessControlType.Allow);
}

The problem is that if you have an account other than System , User , Everyone , Administrator , that permission may remain.问题是,如果您拥有SystemUserEveryoneAdministrator以外的帐户,则该权限可能会保留。 Is there a way to release the permissions of all accounts?有没有办法释放所有账号的权限?

I'm using a translator so I don't know if the meaning is conveyed correctly..我正在使用翻译器,所以我不知道意思是否传达正确..

you can loop through all ruels and then remove them.您可以遍历所有规则,然后将其删除。

var security = Directory.GetAccessControl(path);
var rules = security.GetAccessRules(true, true, typeof(NTAccount));

foreach (FileSystemAccessRule rule in rules)
{
    if (rule.IdentityReference.Value == accountToRemove) 
        security.RemoveAccessRuleSpecific(rule);

}

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

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