如何使用 VB.net 按字母顺序列出 AD 组?

How to list AD group alphabetically using VB.net?

我创建了一个 vb.net 应用程序,其中列出了用户计算机所属的活动目录组。 我不能做或找不到如何在线做的是如何按字母顺序显示广告组列表。 有谁知道如何按字母顺序显示它们? 这是我到目前为止的代码。

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

如有任何帮助,我们将不胜感激!

提前致谢。

我通常会为此去 linq。

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

然后您可以遍历 orderedGroups 而不是 Groups 来获取您需要的内容。