.NET 相当于 "net group /domain" ?



如果我在Windows命令行上运行"net group/domain",我会得到当前域服务器的"全局组"列表。我想从我的.NET代码中获得相同的列表。

在谷歌上搜索,我发现成千上万的"如何获取用户所属的组"、"如何获取属于一个组的用户"等。我只想要所有全局组的列表;而不是与特定用户相关联的那些。有人能给我看一下这方面的代码,或者可以从中派生代码的引用吗?

此代码将查找当前域中的所有全局组:

using (var context = new PrincipalContext(ContextType.Domain))
using (var filter = new GroupPrincipal(context) { GroupScope = GroupScope.Global })
using (var searcher = new PrincipalSearcher(filter))
{
    var groups = searcher.FindAll();
}

最新更新