Powered By Blogger

Thursday, December 31, 2009

Fetch IP address connected in LAN windows c#

IP addresses of Client machines connected in LAN


private void but_fetchIPAddr_Click(object sender, EventArgs e)
{
but_fetchIPAddr.Enabled = false;
DirectoryEntry ParentEntry = new DirectoryEntry();
try
{
ParentEntry.Path = "WinNT:";
foreach (DirectoryEntry childEntry in ParentEntry.Children)
{
TreeNode newNode = new TreeNode(childEntry.Name);
switch (childEntry.SchemaClassName)
{
case "Domain":
TreeNode ParentDomain = new TreeNode(childEntry.Name);
treeView1.Nodes.AddRange(new TreeNode[] { ParentDomain });
DirectoryEntry SubParentEntry = new DirectoryEntry();
SubParentEntry.Path = "WinNT://" + childEntry.Name;
foreach (DirectoryEntry SubChildEntry in SubParentEntry.Children)
{
TreeNode newNode1 = new TreeNode(SubChildEntry.Name);
switch (SubChildEntry.SchemaClassName)
{
case "Computer":
ParentDomain.Nodes.Add(newNode1);
break;
}
}
break;
}
}
}
finally
{
ParentEntry = null;
treeView1.Visible = true;

}
}

private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
txtServer.Text = GetIPAddress(treeView1.SelectedNode.Text);
}


public string GetIPAddress(string CompName)
{

System.Net.IPAddress oAddr = default(System.Net.IPAddress);
string sAddr = null;
try
{
{
oAddr = new System.Net.IPAddress(System.Net.Dns.GetHostByName(CompName).AddressList[0].Address);
sAddr = oAddr.ToString();
}
}
catch (System.Exception Excep)
{
MessageBox.Show("Select the Name of a Client not a Group.");
}
finally
{
}
return sAddr;
}

No comments:

Post a Comment