如何在C#中获取给定文件夹的组和用户名列表

Jim*_*Del 6 c# security permissions

我找到了一些代码,展示了如何获取给定域中的用户列表.但我希望从一个文件夹中获取名称.我的搜索并没有让我走得太远.下面是我正在寻找的列表的示例.

在此输入图像描述

谢谢

Gre*_*reg 5

我不完全确定你打算用上面的列表做什么,但你基本上可以做的是获取预期目录的权限属性。您基本上可以这样查询

// Variables:
string folderPath = "";
DirectoryInfo dirInfo = null;
DirectorySecurity dirSec = null;
int i = 0;

try
{
     // Read our Directory Path.
     do
     {
          Console.Write("Enter directory... ");
          folderPath = Console.ReadLine();
     }
     while (!Directory.Exists(folderPath));

     // Obtain our Access Control List (ACL)
     dirInfo = new DirectoryInfo(folderPath);
     dirSec = dirInfo.GetAccessControl();

     // Show the results.
     foreach (FileSystemAccessRule rule in dirSec.GetAccessRules(true, true, typeof(NTAccount)))
     {
          Console.WriteLine("[{0}] - Rule {1} {2} access to {3}",
          i++,
          rule.AccessControlType == AccessControlType.Allow ? "grants" : "denies",
          rule.FileSystemRights,
          rule.IdentityReference.ToString());
      }
}
catch (Exception ex)
{
     Console.Write("Exception: ");
     Console.WriteLIne(ex.Message);
}

Console.WriteLine(Environment.NewLine + "...");
Console.ReadKey(true);
Run Code Online (Sandbox Code Playgroud)

这是查询目录以获取其权限级别的一个非常基本的示例。您会注意到它还会显示所有关联的实体帐户。这应该会编译并基本上向您显示与目录关联帐户

我不确定这是否是您要问的问题-希望这会有所帮助。