Hey guys, I am writing an Active Directory Tool and I created a Dictionary<> that iterates through all of the available attributes from the LDAP server. The Keys are the names of the attributes (displayname, samaccountname, gn, sn, etc), the TValues are the associated values to the keys. I am able to add the Keys to a TreeView, but I need help adding all of the values as child nodes under it's appropriate Node. Any help would be greatly appreciated.
Here is the code:
private void srchByID()
{
try
{
string Search = txtSearch.Text;
Root = Searcher.DE("contoso.com", "administrator", "system32!");
ADSearch = Searcher.DS(Root);
//ADSearch.Filter = "(&(objectClass=person)(" + SearchType + "=" + Search + "))";
ADSearch.Filter = Searcher.Filter("Person", "SAMAccountName", Search);
SearchResultCollection results = ADSearch.FindAll();
foreach (SearchResult result in results)
{
Dictionary<string, ResultProperty>.KeyCollection kc = BuildResult(result).Keys; //This passes a Dictionary to 'kc'
foreach (string key in kc) //iterate through dictionary and add keys to TreeView.
{
TreeNode node = default(TreeNode);
node = treeView1.Nodes.Add(key);
node.Tag = key;
}
Dictionary<string, ResultProperty>.ValueCollection vc = BuildResult(result).Values;
foreach (ResultProperty value in vc)
{
// This is where I am needing help//
//The code here should be a child node of the keys.
}
ResultPropertyCollection rpc = result.Properties;
txtLogonName.Text = DATA.GetRSString("SAMAccountName", rpc);
txtDisplayName.Text = DATA.GetRSString("DisplayName", rpc);
txtEmail.Text = DATA.GetRSString("Mail", rpc);
txtADsPath.Text = DATA.GetRSString("ADSPath", rpc);
txtTelephoneNumber.Text = DATA.GetRSString("TelephoneNumber", rpc);
txtSecretQuestion.Text = DATA.GetRSString("ExtensionAttribute1", rpc);
txtTitle.Text = DATA.GetRSString("Title", rpc);
txtDepartment.Text = DATA.GetRSString("Department", rpc);
txtHomeDirectory.Text = DATA.GetRSString("HomeDirectory", rpc);
txtBadPasswordCount.Text = DATA.GetRSString("BadPwdCount", rpc);
txtCreatedOn.Text = DATA.GetRSString("WhenCreated", rpc);
txtID.Text = DATA.GetRSString("Company", rpc);
dataGridView1.Rows.Add(DATA.GetRSString("SAMAccountName", rpc), DATA.GetRSString("Mail", rpc), DATA.GetRSString("DisplayName", rpc), DATA.GetRSString("ExtensionAttribute1", rpc), DATA.GetRSString("Company", rpc));
}
if (results.Count == 0) MessageBox.Show("Unable to find: " + "" + Search + "", "Object Not Found", MessageBoxButtons.OK, MessageBoxIcon.Error);
else ADSearch.Dispose();
}
catch (COMException ex)
{
MessageBox.Show("" + ex.Message + "", "Query Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}