tags:

views:

52

answers:

1

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?

A: 

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.

Philip Rieck
Thank you, this does work, but doesn't it defeat the purpose? Won't the "foreach" cause the object to be bound and retrieve all the objects, which are later filtered by the SchemaClassName check?
Will I Am
It does, but the as far as I'm aware, the WinNT provider does not allow searching like the ldap one does (i.e. you can't use a directorysearcher)
Philip Rieck