private void Button_getUserInfo_Click(object sender, System.EventArgs e)
{
try
{
DirectoryEntry   OldUser;   
DirectoryEntry   UserEntry=new   DirectoryEntry   (); 
//写入配置文件获取
UserEntry.Path = "LDAP://172.20.1.51";
UserEntry.Username = @"bingo\xtone";
UserEntry.Password = "123456"; System.DirectoryServices.DirectorySearcher dSearcher = new DirectorySearcher(); DirectorySearcher MySearch = new DirectorySearcher(UserEntry);
MySearch.Filter = ("(&(objectClass=user)(sAMAccountName=" + this.Textbox_UCount.Text.Trim() + "))");
SearchResult myResult = MySearch.FindOne();
string userName ="";
if (myResult.Properties["name"] )
{
 userName = myResult.Properties["name"][0].ToString();//不一定能获得
}
else
{
 userName ="no this user"; } string email = myResult.Properties["mail"][0].ToString();//不一定能获得
string tel = myResult.Properties["telephonenumber"][0].ToString();//不一定能获得 string ouPath =myResult.Properties["adspath"][0].ToString();//肯定能获得
string fullOUDept = ouPath.Substring(ouPath.IndexOf("OU"),ouPath.IndexOf("DC") - ouPath.IndexOf("OU") -1);

string[] arr = fullOUDept.Split(','); string FullPath = "";
for (int i=arr.Length-1;i>=0;i--)
{ FullPath += arr[i].ToString().Replace("OU=","").Trim() + "->"; }
this.TextBox_suozdw.Text = FullPath.Remove(FullPath.Length-2,2); this.TextBox_baoxry.Text = userName;
// this.TextBox_email.Text = email;
// this.TextBox_baoxdh.Text = tel;
}
catch(Exception ex)
{
string err = ex.ToString();
return;
}
finally
{} }