private void buttonQuery1_Click(object sender, EventArgs e) { try { richTextBoxMessage.Clear(); dtAttribute.Clear(); ActiveDirectoryHelper adHelper = new ActiveDirectoryHelper(textBoxPath.Text, textBoxAccount.Text, textBoxPassword.Text); SearchResult result = adHelper.SearchFirstOne(); if (result == null) { richTextBoxMessage.AppendText("No data."); return; } ResultPropertyCollection rpc = result.Properties; List <string> nameList = new List <string>(rpc.PropertyNames.Count); foreach (string name in rpc.PropertyNames) { nameList.Add(name); } nameList = nameList.Distinct().OrderBy(x => x).ToList(); foreach (string name in nameList) { ResultPropertyValueCollection rpvc = rpc[name]; int valueCount = rpvc.Count; foreach (object value in rpvc) { if (value == null) { continue; } string valueString = DirectoryUtility.ExtractAttributValue(name, value); richTextBoxMessage.AppendText(string.Format("{0}={1}\n", name, valueString)); DataRow drAttribute = dtAttribute.NewRow(); drAttribute["Name"] = name; drAttribute["Value"] = valueString; dtAttribute.Rows.Add(drAttribute); } } dataGridViewAttribute.Sort(dataGridViewAttribute.Columns[0], ListSortDirection.Ascending); } catch (Exception ex) { richTextBoxMessage.AppendText(ex.ToString()); } finally { richTextBoxMessage.ScrollToCaret(); } }