我正在学习C#并且是新手.请耐心等待我.
我在C#中开发了一个应用程序,通过将PowerShell命令输入到AD中来搜索AD中的用户,组和组成员.
现在,我正在尝试使用C#中的DirectoryServices来获得相同的结果,但是获得相同结果所需的时间比PowerShell中的时间长得多.
以下是我现在使用DirectoryServices进行快速测试的内容:
using System.DirectoryServices;
using System.DirectoryServices.ActiveDirectory;
private void button1_Click(object sender, EventArgs e)
{
string textbox = textBox1.Text.ToString();
listBox1.Items.Clear();
listView1.Items.Clear();
listView1.Columns.Clear();
try
{
// Bind to the object for which to retrieve property data.
DirectoryEntry de = new DirectoryEntry("");
DirectorySearcher ds = new DirectorySearcher(de);
ds.Filter = "(&(objectClass=Group)(cn="+ textbox + "))";
ds.SearchScope = SearchScope.Subtree;
SearchResultCollection rsAll = ds.FindAll();
listView1.Columns.Add("samsaccountname");
string samsaccountname = "";
foreach (SearchResult searchresult in rsAll)
{
if (searchresult.GetDirectoryEntry().Properties["samaccountname"].Value != null)
{ samsaccountname = searchresult.GetDirectoryEntry().Properties["samaccountname"].Value.ToString(); }
else { …Run Code Online (Sandbox Code Playgroud)