Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 999
JimDel Avatar asked Jul 11 '13 21:07

JimDel


People also ask

How to list the groups a user is member of?

You can run the below command to list the groups a user is member of. This command prints the details of the given user account. You can find the group membership information in the last two line of this command output. net user userName.

Where can I find the list of users created on Windows?

Normally, we can find the list of local users or groups created on a windows system from User Accounts applet in Control Panel, User Accounts in Control Panel. Or, more in detail in Computer Management MMC, which is my favorite place when checking things like this. Users and Groups in Computer Management MMC.

How do I retrieve a list of groups in a directory?

The answer depends on what kind of groups you want to retrieve. The System.DirectoryServices.AccountManagement namespace provides two group retrieval methods: GetGroups - Returns a collection of group objects that specify the groups of which the current principal is a member.

How to list groups on Linux?

In order to list groups on Linux, you have to execute the “cat” command on the “/etc/group” file. When executing this command, you will be presented with the list of groups available on your system. Use one of the following commands to list groups on your system. But what do the columns of the group file even represent?


1 Answers

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.

like image 86
Greg Avatar answered Sep 29 '22 12:09

Greg