private void btnSearch_Click(object sender, EventArgs e) { lvResults.Items.Clear(); dLibAd searchAdObjects = new dLibAd(); PrincipalSearchResult <Principal> resultsUsers = null; PrincipalSearchResult <Principal> resultsGroups = null; string[] searchQuery = rtBoxSearchQuery.Text.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries); switch (searchType) { case 0: foreach (var line in searchQuery) { resultsGroups = searchAdObjects.GetADGroup(new PrincipalContext(ContextType.Domain, frmMain.domainAccountData[0], frmMain.domainAccountData[1], frmMain.domainAccountData[2]), line, false); fillListViewResults(resultsGroups); } break; case 1: foreach (var line in searchQuery) { resultsUsers = searchAdObjects.GetADUser(new PrincipalContext(ContextType.Domain, frmMain.domainAccountData[0], frmMain.domainAccountData[1], frmMain.domainAccountData[2]), line); fillListViewResults(resultsUsers); } break; case 2: foreach (var line in searchQuery) { resultsUsers = searchAdObjects.GetADUser(new PrincipalContext(ContextType.Domain, frmMain.domainAccountData[0], frmMain.domainAccountData[1], frmMain.domainAccountData[2]), line); fillListViewResults(resultsUsers); } foreach (var line in searchQuery) { resultsGroups = searchAdObjects.GetADGroup(new PrincipalContext(ContextType.Domain, frmMain.domainAccountData[0], frmMain.domainAccountData[1], frmMain.domainAccountData[2]), line, false); fillListViewResults(resultsGroups); } break; } if (lvResults.Items.Count > 0) { tbMain.SelectTab(tbpResults); btnOK.Enabled = true; } else { btnOK.Enabled = false; } }
private void txtBoxObject_TextChanged(object sender, EventArgs e) { tvResults.Nodes.Clear(); int qrySize = txtBoxObject.Text.Count(); this.Text = "Seeking for: " + txtBoxObject.Text; if ((txtBoxObject.Text != string.Empty) & (txtBoxObject.Text.Trim().Count() >= 3)) { tvResults.Nodes.Clear(); //Seek Users--------------------------------------------------------- Thread thloadAdUsers = new Thread(() => { try { if (txtBoxObject.Text.Count() == qrySize) { this.Invoke((MethodInvoker) delegate { tSUsers.Image = Seeker.Properties.Resources.smallPBarGreen; }); Thread.CurrentThread.Name = "thloadAdUsers"; TreeNode tGrpNodeUsers = new TreeNode(); if ((!tvResults.Nodes.ContainsKey("users")) & (txtBoxObject.Text.Count() == qrySize)) // & (!txtBoxObject.Text.Contains("(Insira pelo menos 3 Caracteres)"))) { tGrpNodeUsers.Name = "users"; tGrpNodeUsers.Text = "Users"; tGrpNodeUsers.ImageIndex = 0; tGrpNodeUsers.SelectedImageIndex = 0; this.Invoke((MethodInvoker) delegate { tvResults.Nodes.Add(tGrpNodeUsers); }); } int contUsers = 1; var usersItems = new List <TreeNode>(); dLibAd searchUsers = new dLibAd(); using (PrincipalSearchResult <Principal> results = searchUsers.GetADUser(new PrincipalContext(ContextType.Domain, frmMain.domainAccountData[0], frmMain.domainAccountData[1], frmMain.domainAccountData[2]), txtBoxObject.Text.Trim())) { foreach (var foundUser in results) { if (txtBoxObject.Text.Count() == qrySize) { if (foundUser != null) { this.Invoke((MethodInvoker) delegate { TreeNode tGrpNode = new TreeNode(); tGrpNode.Text = foundUser.DisplayName + "," + foundUser.SamAccountName; tGrpNode.ImageIndex = 1; tGrpNode.SelectedImageIndex = 2; //tGrpNodeUsers.Nodes.Add(tGrpNode); usersItems.Add(tGrpNode); tGrpNodeUsers.Text = "Users(" + contUsers++ + ")"; }); } } else { this.Invoke((MethodInvoker) delegate { tSUsers.Image = new Bitmap(1, 1); }); Thread.CurrentThread.Abort(); } foundUser.Dispose(); } this.Invoke((MethodInvoker) delegate { TreeNode[] arr = usersItems.ToArray(); tvResults.BeginUpdate(); tGrpNodeUsers.Nodes.AddRange(arr); tvResults.EndUpdate(); this.Invoke((MethodInvoker) delegate { tSUsers.Image = new Bitmap(1, 1); }); }); } } else { this.Invoke((MethodInvoker) delegate { tSUsers.Image = new Bitmap(1, 1); }); Thread.CurrentThread.Abort(); } } catch { Thread.CurrentThread.Abort(); } }); //------------------------------------------------------------- // Seek Computers --------------------------------------------- Thread thloadAdComputers = new Thread(() => { try { if (txtBoxObject.Text.Count() == qrySize) { this.Invoke((MethodInvoker) delegate { tSComputers.Image = Seeker.Properties.Resources.smallPBarBlue; }); Thread.CurrentThread.Name = "thloadAdComputers"; TreeNode tGrpNodeComputers = new TreeNode(); if ((!tvResults.Nodes.ContainsKey("computers")) & (txtBoxObject.Text.Count() == qrySize)) //& (!txtBoxObject.Text.Contains("(Insira pelo menos 3 Caracteres)"))) { tGrpNodeComputers.Name = "computers"; tGrpNodeComputers.Text = "Computers"; tGrpNodeComputers.ImageIndex = 3; tGrpNodeComputers.SelectedImageIndex = 3; this.Invoke((MethodInvoker) delegate { tvResults.Nodes.Add(tGrpNodeComputers); }); } int contComputers = 1; var computersItems = new List <TreeNode>(); dLibAd searchComputers = new dLibAd(); using (PrincipalSearchResult <Principal> resultsComputers = searchComputers.GetADComputer(new PrincipalContext(ContextType.Domain, frmMain.domainAccountData[0], frmMain.domainAccountData[1], frmMain.domainAccountData[2]), txtBoxObject.Text.Trim())) { foreach (var foundComputer in resultsComputers) { if (txtBoxObject.Text.Count() == qrySize) { if (foundComputer != null) { try { this.Invoke((MethodInvoker) delegate { TreeNode tGrpNode = new TreeNode(); tGrpNode.Text = foundComputer.SamAccountName.Substring(0, foundComputer.SamAccountName.Length - 1) + "," + foundComputer.DisplayName; tGrpNode.ImageIndex = 4; tGrpNode.SelectedImageIndex = 5; //tGrpNodeComputers.Nodes.Add(tGrpNode); computersItems.Add(tGrpNode); tGrpNodeComputers.Text = "Computers(" + contComputers++ + ")"; }); } catch { } } } else { this.Invoke((MethodInvoker) delegate { tSComputers.Image = new Bitmap(1, 1); }); Thread.CurrentThread.Abort(); } foundComputer.Dispose(); } } this.Invoke((MethodInvoker) delegate { //Stopwatch testTime = new Stopwatch(); TreeNode[] arr = computersItems.ToArray(); //testTime.Start(); tvResults.BeginUpdate(); //foreach (ListViewItem item in items) tGrpNodeComputers.Nodes.AddRange(arr); tvResults.EndUpdate(); arr = null; computersItems = null; //testTime.Stop(); this.Invoke((MethodInvoker) delegate { tSComputers.Image = new Bitmap(1, 1); }); }); } else { this.Invoke((MethodInvoker) delegate { tSComputers.Image = new Bitmap(1, 1); }); Thread.CurrentThread.Abort(); } } catch { Thread.CurrentThread.Abort(); } }); //------------------------------------------------------------- //Seek Groups--------------------------------------------------------- Thread thloadAdGroups = new Thread(() => { try { if (txtBoxObject.Text.Count() == qrySize) { this.Invoke((MethodInvoker) delegate { tSGroups.Image = Seeker.Properties.Resources.smallPbarYellow; }); Thread.CurrentThread.Name = "thloadAdGroups"; TreeNode tGrpNodeGroups = new TreeNode(); if ((!tvResults.Nodes.ContainsKey("groups")) & (txtBoxObject.Text.Count() == qrySize)) { tGrpNodeGroups.Name = "groups"; tGrpNodeGroups.Text = "Groups"; tGrpNodeGroups.ImageIndex = 9; tGrpNodeGroups.SelectedImageIndex = 9; this.Invoke((MethodInvoker) delegate { tvResults.Nodes.Add(tGrpNodeGroups); }); } int contGroups = 1; var groupsItems = new List <TreeNode>(); dLibAd searchGroups = new dLibAd(); using (PrincipalSearchResult <Principal> results = searchGroups.GetADGroup(new PrincipalContext(ContextType.Domain, frmMain.domainAccountData[0], frmMain.domainAccountData[1], frmMain.domainAccountData[2]), txtBoxObject.Text.Trim(), false)) { foreach (var foundGroup in results) { if (txtBoxObject.Text.Count() == qrySize) { if (foundGroup != null) { this.Invoke((MethodInvoker) delegate { TreeNode tGrpNode = new TreeNode(); tGrpNode.Text = foundGroup.SamAccountName; tGrpNode.ImageIndex = 10; tGrpNode.SelectedImageIndex = 11; //tGrpNodeGroups.Nodes.Add(tGrpNode); groupsItems.Add(tGrpNode); tGrpNodeGroups.Text = "Groups(" + contGroups++ + ")"; }); } } else { this.Invoke((MethodInvoker) delegate { tSGroups.Image = new Bitmap(1, 1); }); Thread.CurrentThread.Abort(); } foundGroup.Dispose(); } this.Invoke((MethodInvoker) delegate { TreeNode[] arr = groupsItems.ToArray(); tvResults.BeginUpdate(); tGrpNodeGroups.Nodes.AddRange(arr); tvResults.EndUpdate(); this.Invoke((MethodInvoker) delegate { tSGroups.Image = new Bitmap(1, 1); }); }); } } else { this.Invoke((MethodInvoker) delegate { tSGroups.Image = new Bitmap(1, 1); }); Thread.CurrentThread.Abort(); } } catch { Thread.CurrentThread.Abort(); } }); //------------------------------------------------------------- //Seek OUS--------------------------------------------------------- Thread thloadAdOUS = new Thread(() => { try { if (txtBoxObject.Text.Count() == qrySize) { this.Invoke((MethodInvoker) delegate { tSOUs.Image = Seeker.Properties.Resources.smallPbarYellow; }); Thread.CurrentThread.Name = "thloadAdOUS"; DirectoryEntry deBase = new DirectoryEntry("LDAP://" + frmMain.domainAccountData[0], frmMain.domainAccountData[1], frmMain.domainAccountData[2]); string sanitizedOU = txtBoxObject.Text; sanitizedOU = Regex.Replace(sanitizedOU, @"[^\w\.@-]", "Seeker", RegexOptions.None); TreeNode tGrpNodeOus = new TreeNode(); if ((!tvResults.Nodes.ContainsKey("ous")) & (txtBoxObject.Text.Count() == qrySize)) { tGrpNodeOus.Name = "ous"; tGrpNodeOus.Text = "OU's"; tGrpNodeOus.ImageIndex = 13; tGrpNodeOus.SelectedImageIndex = 13; this.Invoke((MethodInvoker) delegate { tvResults.Nodes.Add(tGrpNodeOus); }); } int contGroups = 1; var ousItems = new List <TreeNode>(); dLibAd searchOus = new dLibAd(); using (SearchResultCollection l = searchOus.GetADOu(deBase, sanitizedOU)) { foreach (SearchResult foundOu in l) { if (txtBoxObject.Text.Count() == qrySize) { if (foundOu != null) { this.Invoke((MethodInvoker) delegate { TreeNode tGrpNode = new TreeNode(); tGrpNode.Text = tGrpNode.Text = foundOu.Properties["name"][0].ToString() + "-" + foundOu.Properties["distinguishedName"][0].ToString(); tGrpNode.Tag = foundOu.Properties["distinguishedName"][0].ToString(); tGrpNode.ImageIndex = 14; tGrpNode.SelectedImageIndex = 15; //tGrpNodeOus.Nodes.Add(tGrpNode); ousItems.Add(tGrpNode); tGrpNodeOus.Text = "OU's(" + contGroups++ + ")"; }); } } else { this.Invoke((MethodInvoker) delegate { tSOUs.Image = new Bitmap(1, 1); }); Thread.CurrentThread.Abort(); } } } this.Invoke((MethodInvoker) delegate { TreeNode[] arr = ousItems.ToArray(); tvResults.BeginUpdate(); tGrpNodeOus.Nodes.AddRange(arr); tvResults.EndUpdate(); this.Invoke((MethodInvoker) delegate { tSOUs.Image = new Bitmap(1, 1); }); }); deBase.Dispose(); } else { this.Invoke((MethodInvoker) delegate { tSOUs.Image = new Bitmap(1, 1); }); Thread.CurrentThread.Abort(); } } catch { Thread.CurrentThread.Abort(); } }); //------------------------------------------------------------- //Threads Start------------------------------------------------ thloadAdUsers.Start(); thloadAdComputers.Start(); thloadAdGroups.Start(); thloadAdOUS.Start(); //------------------------------------------------------------- } else { if (txtBoxObject.Text.Count() == 0) { this.Text = "What are you Seeking Today ?"; txtBoxObject.SelectionStart = 0; txtBoxObject.SelectionLength = txtBoxObject.Text.Length; } } tvResults.TreeViewNodeSorter = new NodeSorterByName(); tvResults.Sort(); }