如何获得所有的Windows组?

我写这个来获得一个特定用户所属的组:

DirectoryEntry AD = new DirectoryEntry("WinNT://" + Environment.MachineName + ",computer"); DirectoryEntry user = AD.Children.Find(completeUserName, "user"); object obGroups = AD.Invoke("Groups"); foreach (object ob in (IEnumerable)obGroups) { // Create object for each group. DirectoryEntry obGpEntry = new DirectoryEntry(ob); listOfMyWindowsGroups.Add(obGpEntry.Name); } for (int j = 0; j < listOfMyWindowsGroups.Count; j++) { //ex } 

如何检索窗口中的所有组,而不仅仅是一个特定的用户?

Solutions Collecting From Web of "如何获得所有的Windows组?"

如何为组设置过滤器并枚举结果?

试试这个过滤器:

 AD.Children.SchemaFilter.Add("group"); 

试试这个,它会给你所有的团体在一个特定的OU。

 public ArrayList GetGroups() { ArrayList myItems = new ArrayList(); // Create the principal context for the group object. PrincipalContext oPrincipalContext = new PrincipalContext(ContextType.Domain, sDomain, sDefaultOU, ContextOptions.SimpleBind, sServiceUser, sServicePassword); // Create the GroupPrincipal object and set the diplay name property. GroupPrincipal oGroupPrincipal = new GroupPrincipal(oPrincipalContext); // Create a PrincipalSearcher object. PrincipalSearcher oPrincipalSearcher = new PrincipalSearcher(oGroupPrincipal); // Searches for all groups named "Administrators". PrincipalSearchResult<Principal> oPrincipalSearchResult = oPrincipalSearcher.FindAll(); foreach (Principal oResult in oPrincipalSearchResult) { myItems.Add(oResult.Name); } return myItems; } 

为了充分的参考,你可以检查这一个

.Net 3.5版本 – > http://anyrest.wordpress.com/2010/06/28/active-directory-c/

旧版本 – > http://anyrest.wordpress.com/2010/02/01/active-directory-objects-and-c/