如何显示域中文件夹目录中用户的访问权限

时间:2019-05-27 08:17:41

标签: c#

我试图在我的AD服务器文件共享上显示每个文件夹的用户权限,但没有显示

这是我正在尝试的代码:

DirectorySecurity dSecurity = Directory.GetAccessControl(Dir);
foreach (FileSystemAccessRule rule in dSecurity.GetAccessRules(true, true, typeof(System.Security.Principal.NTAccount)))
{
    if (rule.FileSystemRights == FileSystemRights.Read)
    {
        ad_treeView_trv.Nodes.Add(Dir);
    }
}  

这是文件夹选择的完整代码:

private void Ad_folder_select_btn_Click(object sender, EventArgs e)
{
    FolderBrowserDialog folderBrowserDialog1 = new FolderBrowserDialog();
    folderBrowserDialog1.SelectedPath = ad_folder_select_txtBox.Text;
    DialogResult drResult = folderBrowserDialog1.ShowDialog();
    if (drResult == System.Windows.Forms.DialogResult.OK)
        ad_folder_select_txtBox.Text = folderBrowserDialog1.SelectedPath;
}

private void Ad_folder_load_btn_Click(object sender, EventArgs e)
{
    // Setting Inital Value of Progress Bar  
    ad_progressBar_prg.Value = 0;
    // Clear All Nodes if Already Exists  
    ad_treeView_trv.Nodes.Clear();
    ad_toolTip_tlt.ShowAlways = true;
    if (ad_folder_select_txtBox.Text != "" && Directory.Exists(ad_folder_select_txtBox.Text))
        LoadDirectory(ad_folder_select_txtBox.Text);
    else
        MessageBox.Show("Select Directory!!");
}

public void LoadDirectory(string Dir)
{
    DirectoryInfo di = new DirectoryInfo(Dir);
    //Setting ProgressBar Maximum Value  
    ad_progressBar_prg.Maximum = Directory.GetFiles(Dir, "*.*", SearchOption.AllDirectories).Length + Directory.GetDirectories(Dir, "**", SearchOption.AllDirectories).Length;
    TreeNode tds = ad_treeView_trv.Nodes.Add(di.Name);
    tds.Tag = di.FullName;
    tds.StateImageIndex = 0;
    LoadFiles(Dir, tds);
    LoadSubDirectories(Dir, tds);
    DirectorySecurity dSecurity = Directory.GetAccessControl(Dir);
    foreach (FileSystemAccessRule rule in dSecurity.GetAccessRules(true, true, typeof(System.Security.Principal.NTAccount)))
    {
        if (rule.FileSystemRights == FileSystemRights.Read)
        {
            ad_treeView_trv.Nodes.Add(Dir);
        }
    }
}

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.StateImageIndex = 0;
        tds.Tag = di.FullName;
        LoadFiles(subdirectory, tds);
        LoadSubDirectories(subdirectory, tds);
        UpdateProgress();
    }
}

private void LoadFiles(string dir, TreeNode td)
{
    string[] Files = Directory.GetFiles(dir, "*.*");

    // Loop through them to see files  
    foreach (string file in Files)
    {
        FileInfo fi = new FileInfo(file);
        TreeNode tds = td.Nodes.Add(fi.Name);
        tds.Tag = fi.FullName;
        tds.StateImageIndex = 1;
        UpdateProgress();
    }
}

private void UpdateProgress()
{
    if (ad_progressBar_prg.Value < ad_progressBar_prg.Maximum)
    {
        ad_progressBar_prg.Value++;
        int percent = (int)(((double)ad_progressBar_prg.Value / (double)ad_progressBar_prg.Maximum) * 100);
        ad_progressBar_prg.CreateGraphics().DrawString(percent.ToString() + "%", new Font("Arial", (float)8.25, FontStyle.Regular), Brushes.Black, new PointF(ad_progressBar_prg.Width / 2 - 10, ad_progressBar_prg.Height / 2 - 7));

        Application.DoEvents();
    }
}

在示例中,我需要显示用户对文件夹的访问权限

  1. 公司政策 一种。用户1 b。用户2 1.1 IT政策    一种。用户1    b用户2
  2. 公司表格 一种。用户3 b。用户4 2.2 IT表格    一种。用户3    b。用户4 等等...

此刻,它仅显示文件夹而不是用户权限

谢谢

0 个答案:

没有答案