可以将路径指定为"WinNT:"枚举出一个局域网内所有的域,如下列代码所示: DirectoryEntry objSpace = new DirectoryEntry("WinNT:"); 
foreach(DirectoryEntry domain in objSpace.Children) 
System.Diagnostics.Debug.WriteLine(domain.Name); 如果要枚举一个组中的所有成员,首先要激活其Members成员,并查询(映射到)他的IEnumerable接口。然后遍历其中每一个元素,生成相应的入口对象并输出。如下列代码所示: string domainName = "Domain"; 
string groupName = "Group"; 
DirectoryEntry group = new DirectoryEntry("WinNT://" + domainName + "/" + groupName + ",group"); foreach(Object member in (IEnumerable)group.Invoke("Members")) 

DirectoryEntry dirmem = new DirectoryEntry(member); 
Console.WriteLine(dirmem.Name); 

解决方案 »

  1.   

    '======================================================================================
        ' GetAllGroups
        ' WriteBy:Singhua    
        ' Description: get one domain's all groups
        '              得到一個網域下所有的群組
        ' Arguments: attributes of the object:
        '  DcServer - the server keep the groups
        '  return: an array of all the groups
        ' modify history:
        '======================================================================================
        Function GetAllGroups(ByVal DcServer As String) As Object
            DcServer = Trim(DcServer)
            Dim myDomain
            Dim group
            Dim str As String        DcServer = "WinNT://" & DcServer        myDomain = GetObject(DcServer)        Dim filter(0), oGroups() As Object
            filter(0) = "group"        myDomain.Filter = filter   'if filter = {"group","user"} then show all the user
            For Each group In myDomain
                str = str & "^" & group.Name
            Next        str = Right(str, str.Length - 1)
            oGroups = Split(str, "^")
            System.Array.Sort(oGroups)        GetAllGroups = oGroups
        End Function    '======================================================================================
        ' GetAllUserInGroup
        ' WriteBy:Singhua    
        ' Description: get all users in one group
        '              得到一個群組下所有的人員
        ' Arguments: attributes of the object:
        '  DcServer - the server keep the groups
        '  GroupName - the group name your want to get user
        '  return: an array of all users in the group
        ' modify history:
        '======================================================================================
        Function GetAllUserInGroup(ByVal DcServer As String, ByVal GroupName As String, _
                                   ByVal Domain As String) As Object
            DcServer = Trim(DcServer)
            GroupName = Trim(GroupName)
            Domain = LCase(Trim(Domain))        Dim myGroup
            Dim user
            Dim str As String
            Dim Members, oUsers() As Object
            Dim i, m As Integer        DcServer = "WinNT://" & DcServer & "/" & GroupName & ",group"
            Try
                myGroup = GetObject(DcServer)
            Catch ex As Exception
                GetAllUserInGroup = Nothing
                Exit Function
            End Try        Members = myGroup.Members
            For Each user In Members
                str = str & "^" & LCase(user.Name)
            Next        str = Right(str, str.Length - 1)
            oUsers = Split(str, "^")
            System.Array.Sort(oUsers)        m = UBound(oUsers)
            For i = 0 To m
                oUsers(i) = Domain & "\" & oUsers(i)
            Next        GetAllUserInGroup = oUsers
        End Function
    參考一下,MSDN中有例子
    希望可以幫到你