你好,我被困在试图添加一个函数到我的Windows窗体程序中,允许用户在文本框中键入他们想要搜索的计算机或计算机活动目录。用户将在文本框中输入搜索字符串,然后点击按钮,匹配该搜索结果的计算机将出现在单独的搜索框中。这是我的代码到目前为止。使用用户输入搜索Active Directory中的计算机名称
我也希望每个计算机名是在单独一行如:
computername1
computername2
computername3
谢谢!
这是里面的按钮看起来像:
List<string> hosts = new List<string>();
DirectoryEntry de = new DirectoryEntry();
de.Path = "LDAP://servername";
try
{
string adser = txtAd.Text; //textbox user inputs computer to search for
DirectorySearcher ser = new DirectorySearcher(de);
ser.Filter = "(&(ObjectCategory=computer)(cn=" + adser + "))";
ser.PropertiesToLoad.Add("name");
SearchResultCollection results = ser.FindAll();
foreach (SearchResult res in results)
//"CN=SGSVG007DC"
{
string computername = res.GetDirectoryEntry().Properties["Name"].Value.ToString();
hosts.Add(computername);
//string[] temp = res.Path.Split(','); //temp[0] would contain the computer name ex: cn=computerName,..
//string adcomp = (temp[0].Substring(10));
//txtcomputers.Text = adcomp.ToString();
}
txtcomputers.Text = hosts.ToString();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
de.Dispose();//Clean up resources
}
你应该[接受你以前的一些问题的答案](http://meta.stackexchange.com/q/5234/153998)。这表示你对那些花时间帮助你的人表示感谢,并且这会提高他们回答你可能遇到的任何问题的可能性。 –