Question

I'd like to retrieve all local groups on my machine (Vista in a W2k3 domain).

If I run:

using (DirectoryEntry de = new DirectoryEntry("WinNT://" + Environment.MachineName + ",group", null, null, AuthenticationTypes.Secure))
{

}

it throws an "unknown error" 0x80005000 which apparently means "invalid path"

However querying for computers (change ,group to ,computer) doesn't raise an error, but it seems to be ignored (it returns all objects? I haven't fully examined the result). ,user also raises the error.

So my question is, am I on the right path? is there a way to apply a filter so I don't retrieve everything? If so, where can I find the correct syntax?

Was it helpful?

Solution

I believe you need to get the machine - groups are a child of that.

Try

DirectoryEntry machine = new DirectoryEntry("WinNT://" + Environment.MachineName + ",Computer");
foreach (DirectoryEntry child in machine.Children)
{
    if (child.SchemaClassName == "Group")
    {
        Debug.WriteLine(child.Name);
    }
}

Feel free to spice it up with some LINQ, but this should give you the base idea.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top