Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 有没有办法在子目录中加载文件夹权限_C#_Directory Security - Fatal编程技术网

C# 有没有办法在子目录中加载文件夹权限

C# 有没有办法在子目录中加载文件夹权限,c#,directory-security,C#,Directory Security,文件夹权限似乎只显示在根文件夹中,而不显示在该根文件夹的子目录中 测试应用程序时,我会获取根文件夹并显示文件夹权限。但是,它还显示根文件夹中的子目录权限,我希望它显示在子目录下。我已经搜索了很多论坛,但找不到解决方案 这是我用于加载目录和获取文件夹权限的代码: public void LoadDirectory(string Dir) { DirectoryInfo di = new DirectoryInfo(Dir); TreeNode tds =

文件夹权限似乎只显示在根文件夹中,而不显示在该根文件夹的子目录中

测试应用程序时,我会获取根文件夹并显示文件夹权限。但是,它还显示根文件夹中的子目录权限,我希望它显示在子目录下。我已经搜索了很多论坛,但找不到解决方案

这是我用于加载目录和获取文件夹权限的代码:

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;
        }
    }
+ Folder 1 
|
|--My-PC\Administrator
|--My-PC\User
对于子目录,我有以下代码:

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);
                }
            }
        }
    }
因此,要给出一个加载具有权限的目录的示例:

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;
        }
    }
+ 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