Example #1
0
        private void buttonQueryAll_Click(object sender, EventArgs e)
        {
            try
            {
                richTextBoxMessage.Clear();

                ActiveDirectoryHelper  adHelper = new ActiveDirectoryHelper(textBoxPath.Text, textBoxAccount.Text, textBoxPassword.Text);
                SearchResultCollection results  = adHelper.SearchAll();

                if (results == null)
                {
                    richTextBoxMessage.AppendText("No data.");
                    return;
                }

                foreach (SearchResult result in results)
                {
                    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));
                        }
                    }

                    richTextBoxMessage.AppendText("\n----------------------------------------------------------\n\n");
                    richTextBoxMessage.ScrollToCaret();
                }

                richTextBoxMessage.AppendText($"Count: {results.Count}");
            }
            catch (Exception ex)
            {
                richTextBoxMessage.AppendText(ex.ToString());
            }
            finally
            {
                richTextBoxMessage.ScrollToCaret();
            }
        }