简体   繁体   中英

How to list AD group alphabetically using VB.net?

I have created a vb.net app that within it lists the active directory groups a users computer is a member of. What I cannot do or find how to do online is how to show that list of AD groups alphabetically. Does anyone know how to show them in alphabetical order? Here is the code I have thus far.

Public Shared Function WorkstationADGroups(ByVal PCName As String) As String

    ' Returns list of AD Groups the comptuer is a member of
    Try
        Dim x As Integer = 1
        Dim result As String = Nothing
        Using ctx As New PrincipalContext(ContextType.Domain)
            Using p = Principal.FindByIdentity(ctx, PCName)
                If Not p Is Nothing Then
                    Dim groups = p.GetGroups()
                    Using groups
                        For Each group In groups
                            result = result & "</BR>" & x & ".     --     " & group.SamAccountName
                            x = x + 1
                        Next
                    End Using
                End If
            End Using
        End Using
        Return result
    Catch ex As Exception
        Return ex.Message
    End Try

End Function

Any help would be greatly appreciated!

Thanks in advance.

I usually go to linq for this.

Dim orderedGroups = (From g In Groups Order By g.SamAccountName)

Then you can loop through orderedGroups instead of Groups to get what you need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM