0

我正在扫描文件夹中的文件,我需要以任何方式获取当前用户的每个文件的权限以表示为字符串。因此,对于附加文件的属性,我想表示类似fmrw 字符串的内容。

我试图在我的扫描循环中完成这个,但是得到空字符串。你能暗示我应该从这里去哪里还是选择另一种方法?提前致谢!

            string permissionShort = string.Empty;
            DirectorySecurity dSecurity = Directory.GetAccessControl(_directory.FullName);
            foreach (FileSystemAccessRule rule in dSecurity.GetAccessRules(true, true, typeof(NTAccount)))
            {
                if (rule.FileSystemRights == FileSystemRights.Write)
                    permissionShort = permissionShort + "x";
                if (rule.FileSystemRights == FileSystemRights.Read)
                    permissionShort = permissionShort + "x";
                if (rule.FileSystemRights == FileSystemRights.AppendData)
                    permissionShort = permissionShort + "x";
                if (rule.FileSystemRights == FileSystemRights.Modify)
                    permissionShort = permissionShort + "x";
                if (rule.FileSystemRights == FileSystemRights.ExecuteFile)
                    permissionShort = permissionShort + "x";
            }
4

1 回答 1

0

FileSystemRights 是一个标记的枚举,这意味着您必须执行 AND 操作来测试枚举的单个值。

    string scanPath = @"C:\Test";
    string permissionShort = string.Empty;
    DirectorySecurity dSecurity = Directory.GetAccessControl(scanPath);
    foreach (FileSystemAccessRule rule in dSecurity.GetAccessRules(true, true, typeof(NTAccount)))
    {
        permissionShort += rule.FileSystemRights.ToString() + " : ";
        permissionShort += ((rule.FileSystemRights & FileSystemRights.FullControl) == FileSystemRights.FullControl) ? "f" : "-";
        permissionShort += ((rule.FileSystemRights & FileSystemRights.Write)== FileSystemRights.Write) ? "w" : "-";
        permissionShort += ((rule.FileSystemRights & FileSystemRights.Read) == FileSystemRights.Read) ? "r" : "-";
        permissionShort += ((rule.FileSystemRights & FileSystemRights.AppendData) == FileSystemRights.AppendData) ? "a" : "-";
        permissionShort += ((rule.FileSystemRights & FileSystemRights.Modify) == FileSystemRights.Modify) ? "m" : "-";
        permissionShort += ((rule.FileSystemRights & FileSystemRights.ExecuteFile) == FileSystemRights.ExecuteFile) ? "e" : "-";
        permissionShort += "\n";
    }
于 2017-02-16T00:06:59.273 回答