JimDel
JimDel

Reputation: 4359

How can I get a list of Groups and User names for a given folder in C#

I found some code that shows how to get a list of users in a given domain. But I'm looking to get the names from just a single folder. My searches aren't getting me very far. Below is an example of the list I'm looking for.

enter image description here

Thanks

Upvotes: 7

Views: 3622

Answers (1)

Greg
Greg

Reputation: 11480

I'm not entirely sure what you intend to do with the above list, but what you could essentially do is obtain Permission Attributes to the intended directory. You could essentially query like this:

// 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);

This is a very basic example of querying the Directory to obtain the permission levels on it. You'll note that it will show all the physical accounts associated as well. This should compile and will essentially show you Accounts Associated to the Directory.

I'm not sure if this is what you were asking about- Hope this helps.

Upvotes: 7

Related Questions