您好,我一直在尝试向我的 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
}