如何获得本地组中的所有用户(性能良好)

Roc*_*cko 3 c# windows

我一直在寻找解决方案,但我找到的所有解决方案都很慢。我想获取本地 Windows 组中的所有用户。该组当然也可以包含 AD 组。所以结果应该包含对组本身的成员的所有用户所包含的AD组的用户。您知道性能良好的解决方案吗?

Ray*_*und 6

您是否尝试过此操作,此示例获取本地计算机中的管理员组成员

using System;
using System.DirectoryServices.AccountManagement;
using System.Collections;

class Program
{
    static void Main(string[] args)
    {
        ArrayList myGroups = GetGroupMembers("Administrators");
        foreach (string item in myGroups)
        {
            Console.WriteLine(item);
        }
        Console.ReadLine();
    }

    public static ArrayList GetGroupMembers(string sGroupName)
    {
        ArrayList myItems = new ArrayList();
        GroupPrincipal oGroupPrincipal = GetGroup(sGroupName);

        PrincipalSearchResult<Principal> oPrincipalSearchResult = oGroupPrincipal.GetMembers();

        foreach (Principal oResult in oPrincipalSearchResult)
        {
            myItems.Add(oResult.Name);
        }
        return myItems;
    }

    public static GroupPrincipal GetGroup(string sGroupName)
    {
        PrincipalContext oPrincipalContext = GetPrincipalContext();

        GroupPrincipal oGroupPrincipal = GroupPrincipal.FindByIdentity(oPrincipalContext, sGroupName);
        return oGroupPrincipal;
    }

    public static PrincipalContext GetPrincipalContext()
    {
        PrincipalContext oPrincipalContext = new PrincipalContext(ContextType.Machine);
        return oPrincipalContext;
    }

}
Run Code Online (Sandbox Code Playgroud)

http://anyrest.wordpress.com