Monday, June 30, 2008

Get list of Network Machines Using C#.NET

Before using this code make a reference to following Namespace

using System.DirectoryServices;

#region Get all the machines in the Network
public ArrayList getNetworkMachines()
{
ArrayList MachineList = new ArrayList();
//Getting the List of Mahcines in Network
DirectoryEntry parentEntry = new DirectoryEntry();
try
{

parentEntry.Path = "WinNT:";
foreach (DirectoryEntry childentry in parentEntry.Children)
{
switch (childentry.SchemaClassName)
{
case "Domain":
DirectoryEntry SubParentEntry = new DirectoryEntry();
SubParentEntry.Path = "WinNT://" + childentry.Name;
foreach (DirectoryEntry subChildEntry in SubParentEntry.Children)
{
switch (subChildEntry.SchemaClassName)
{
case "Computer":
MachineList.Add(subChildEntry.Name);
break;
}
}
break;
}
}
}
catch
{
throw;
}
finally
{
parentEntry = null;
}
//
return MachineList;
}
#endregion

No comments: