Ive been experimenting with ways to discover computers on the network.
Most of what I have tried is centered around looping through our network
address ranges to discover the names of the computers up and running on
the network.
I have tried this using UDP sent to port 136 and retrieving the name of
the machine. I have also attempted doing this using WMI.
The problem I had with UDP is that it isnt reliable, and can be quite
slow when getting replies from a wireless host and I wind up loosing
results.
WMI works reliably, but is very slow when the ManagementScope object
returns with an exception. It appears when it cannot make a connection
to an IP address it takes 42 seconds for it to return and move on to the
next address. Is there a way to speed this process up? Would I be
better off throwing each of these IP address querys into its own thread
and waiting for all the threads to return???
This is the code I have now:
ManagementScope mS;
ObjectQuery oQ;
ManagementObjectSearcher search;
ManagementObjectCollection result;
ConnectionOptions options = new ConnectionOptions();
options.Timeout = TimeSpan.FromSeconds(10);
// Scan the network
for (int i = 1; i < 255; i++)
{
mS = new ManagementScope("\\\\10.1.54." + i.ToString() +
"\\root\\cimv2", options); Console.WriteLine("IP Address
Connecting To: 10.1.54.{0} at {1} Timeout {2}",
i.ToString(), DateTime.Now.ToString(),
options.Timeout.TotalSeconds.ToString());
try
{
mS.Connect();
}
catch
{
Console.WriteLine("Error!!!! at {0}",
DateTime.Now.ToString());
}
if (mS.IsConnected == true)
{
oQ = new ObjectQuery("select * from
Win32_ComputerSystem"); search = new
ManagementObjectSearcher(mS, oQ);
result = search.Get();
foreach (ManagementObject info in result)
{
Console.WriteLine("Computer name {0}",
info["Name"]);
}
oQ = null;
search = null;
result = null;
}
mS = null;
}