Monday, January 18, 2010

Loading AD into TreeView in C#


Been working on a network pinger application much like SolarWinds/What's Up Gold.

A portion of code that may be useful is the loading AD into a tree:

Forgive the hasty code:

private void Connect()
{
try
{
Base = new DirectoryEntry("LDAP://" + this.connectionString);

if (Base != null)
{
treePing.Nodes.Clear();
treePing.BeginUpdate();

TreeNode childNode = treePing.Nodes.Add(Base.Name);
childNode.Tag = Base;

try
{

foreach (DirectoryEntry rootIter in Base.Children)
{

TreeNode RootNode = childNode.Nodes.Add(rootIter.Name);
RootNode.Tag = rootIter;

}
}
finally
{

childNode.Expand();
treePing.EndUpdate();
}
}
}
catch (Exception ex)
{
mnuNotify.ShowBalloonTip(5000, "NetPinger 2010", ex.Message, ToolTipIcon.Error);
Connect2();
}

}
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{

if (e.Node.Nodes.Count == 0)
{

try
{
DirectoryEntry parent = (DirectoryEntry)e.Node.Tag;


if (parent != null)
{

if (parent.Children != null)
{

foreach (DirectoryEntry Iter in parent.Children)
{

TreeNode childNode = e.Node.Nodes.Add(Iter.Name);
childNode.Tag = Iter;


}

}
}
}

catch (System.Exception ex)
{
mnuNotify.ShowBalloonTip(5000,"NetPinger 2010",ex.Message,ToolTipIcon.Error);
}
//Fill the ListView Element
try
{

DirectoryEntry list = (DirectoryEntry)e.Node.Tag;

if (list != null)
{

ctr_list.Clear();

//Add some information to ListView ELement
ctr_list.Columns.Add("Attribute", 90, HorizontalAlignment.Left);
ctr_list.Columns.Add("Value", 350, HorizontalAlignment.Left);


foreach (object listIter in list.Properties.PropertyNames)
{

foreach (Object Iter in list.Properties[listIter.ToString()])
{
System.Windows.Forms.ListViewItem item =
new System.Windows.Forms.ListViewItem(listIter.ToString(), 0);


item.SubItems.Add(Iter.ToString());

ctr_list.Items.AddRange(new ListViewItem[] { item });


}

}
}
}
catch (System.Exception ex)
{

MessageBox.Show(ex.Message);
}

this.selectMe();

toolStripStatusLabel1.Text = treePing.SelectedNode.Text.Substring(3) + " Selected.";
}

}

No comments:

Post a Comment

Generating "Always On Top" NSWindow in macOS across all detected displays

Also: Using UIKit & Cocoa Frameworks using Objective-C In m acOS or OS X , written in either Objective-C or Swift  Langues, you m...