文件夹权限似乎仅显示在根文件夹中,而不显示在该根文件夹的子目录中。
在测试应用程序时,我得到了根文件夹并显示了文件夹权限。但是,它也会在根文件夹中显示子目录权限,我希望它显示在子目录下。我搜索了很多论坛,但找不到解决方案。
这是我用于加载目录和获取文件夹权限的代码:
public void LoadDirectory(string Dir)
{
DirectoryInfo di = new DirectoryInfo(Dir);
TreeNode tds = ad_treeView_view.Nodes.Add(di.Name);
tds.Tag = di.FullName;
tds.StateImageIndex = 0;
LoadFiles(Dir, tds);
LoadSubDirectories(Dir, tds);
DirectorySecurity acl = di.GetAccessControl();
AuthorizationRuleCollection rules = acl.GetAccessRules(true, true, typeof(NTAccount));
WindowsIdentity currentUser = WindowsIdentity.GetCurrent();
WindowsPrincipal principal = new WindowsPrincipal(currentUser);
foreach (AuthorizationRule rule in rules)
{
FileSystemAccessRule fsAccessRule = rule as FileSystemAccessRule;
if (fsAccessRule == null)
continue;
if ((fsAccessRule.FileSystemRights & FileSystemRights.FullControl) > 0)
{
NTAccount ntAccount = rule.IdentityReference as NTAccount;
if (ntAccount == null)
{
continue;
}
if (principal.IsInRole(ntAccount.Value))
{
tds = ad_treeView_view.Nodes.Add("Current user is in role of {0}, has full access", ntAccount.Value);
continue;
}
tds = ad_treeView_view.Nodes.Add("Current user is not in role of {0}, does not have full access", ntAccount.Value);
}
ad_progressBar_prg.Maximum = Directory.GetFiles(Dir, "*.*", SearchOption.AllDirectories).Length + Directory.GetDirectories(Dir, "**", SearchOption.AllDirectories).Length;
}
}
对于子目录,我有以下代码:
private void LoadSubDirectories(string dir, TreeNode td)
{
// Get all subdirectories
string[] subdirectoryEntries = Directory.GetDirectories(dir);
// Loop through them to see if they have any other subdirectories
foreach (string subdirectory in subdirectoryEntries)
{
DirectoryInfo di = new DirectoryInfo(subdirectory);
TreeNode tds = td.Nodes.Add(di.Name);
tds.Tag = di.FullName;
tds.StateImageIndex = 0;
LoadSubDirectories(subdirectory, tds);
UpdateProgress();
DirectorySecurity acl = di.GetAccessControl();
AuthorizationRuleCollection rules = acl.GetAccessRules(true, true, typeof(NTAccount));
WindowsIdentity currentUser = WindowsIdentity.GetCurrent();
WindowsPrincipal principal = new WindowsPrincipal(currentUser);
foreach (AuthorizationRule rule in rules)
{
FileSystemAccessRule fsAccessRule = rule as FileSystemAccessRule;
if (fsAccessRule == null)
continue;
if ((fsAccessRule.FileSystemRights & FileSystemRights.FullControl) > 0)
{
NTAccount ntAccount = rule.IdentityReference as NTAccount;
if (ntAccount == null)
{
continue;
}
if (principal.IsInRole(ntAccount.Value))
{
ad_treeView_view.Nodes.Add("Current user is in role of {0}, has full access", ntAccount.Value);
continue;
}
ad_treeView_view.Nodes.Add("Current user is not in role of {0}, does not have full access", ntAccount.Value);
}
}
}
}
因此,举一个带有权限加载目录的示例:
+ Folder 1
|
|--My-PC\Administrator
|--My-PC\User
当我尝试加载子目录时,我得到以下信息:
+ Folder 1
|
|--+Folder 2
|--+Folder 3
|
|--My-PC\Administrator
|--My-PC\User
|--My-PC\Administrator
|--My-PC\User
|--My-PC\Administrator
|--My-PC\User
我想要实现的目标:
+ Folder 1
|
|--+Folder 2
| |
| |--My-PC\Administrator
| |--My-PC\User
|
|--+Folder 3
| |
| |--My-PC\Administrator
| |--My-PC\User
|
|--My-PC\Administrator
|--My-PC\User