搜尋群組
本主題示範如何使用 DirectorySearcher 搜尋群組。
下列 C# 範例示範如何搜尋網域上的所有群組。
using System.DirectoryServices;
...
DirectorySearcher src = new DirectorySearcher(ou,"(objectCategory=group)");
foreach(SearchResult res in src.FindAll())
{
Console.WriteLine(res.Path);
}
下列 C# 範例示範如何搜尋所有啟用安全性的群組。對於此搜尋,請使用 COM Interop使用 COM Interop 存取 ADSI。如需詳細資訊,請參閱 MSDN Library (https://go.microsoft.com/fwlink/?LinkID=27252 (本頁面可能為英文)) 中的<Visual Basic 與 Visual C# 中的互通性>主題。此範例使用位元搜尋。
using System.DirectoryServices;
...
DirectorySearcher src = new DirectorySearcher(ou,"(objectCategory=group)");
int val = (int) ActiveDs.ADS_GROUP_TYPE_ENUM.ADS_GROUP_TYPE_SECURITY_ENABLED;
string query = "(&(objectCategory=group)(groupType:1.2.840.113556.1.4.804:=" + val.ToString() + "))";
src.Filter = query;
foreach(SearchResult res in src.FindAll())
{
Console.WriteLine(res.Path);
}
下列 C# 範例示範如何搜尋所有全域網域群組,不論它們是安全或不安全群組。對於此搜尋,請使用 COM Interop使用 COM Interop 存取 ADSI。如需詳細資訊,請參閱 MSDN Library (https://go.microsoft.com/fwlink/?LinkID=27252 (本頁面可能為英文)) 中的<Visual Basic 與 Visual C# 中的互通性>主題。
using System.DirectoryServices;
...
DirectorySearcher src = new DirectorySearcher(ou,"(objectCategory=group)");
int val = (int) ActiveDs.ADS_GROUP_TYPE_ENUM.ADS_GROUP_TYPE_GLOBAL_GROUP;
string query = "(&(objectCategory=group)(groupType:1.2.840.113556.1.4.804:=" + val.ToString() + "))";
src.Filter = query;
foreach(SearchResult res in src.FindAll())
{
Console.WriteLine(res.Path);
}
下列 C# 範例示範如何搜尋所有全域網域安全群組。對於此搜尋,請使用 COM Interop使用 COM Interop 存取 ADSI。如需詳細資訊,請參閱 MSDN Library (https://go.microsoft.com/fwlink/?LinkID=27252 (本頁面可能為英文)) 中的<Visual Basic 與 Visual C# 中的互通性>主題。
[C#]
using System.DirectoryServices;
...
DirectorySearcher src = new DirectorySearcher(ou,"(objectCategory=group)");
int val = (int) (ActiveDs.ADS_GROUP_TYPE_ENUM.ADS_GROUP_TYPE_GLOBAL_GROUP
| ActiveDs.ADS_GROUP_TYPE_ENUM.ADS_GROUP_TYPE_SECURITY_ENABLED);
string query = "(&(objectCategory=group)(groupType=" + val.ToString() + "))";
src.Filter = query;
foreach(SearchResult res in src.FindAll())
{
Console.WriteLine(res.Path);
}
請參閱
參考
DirectorySearcher
System.DirectoryServices
概念
Send comments about this topic to Microsoft.
Copyright © 2007 by Microsoft Corporation.All rights reserved.