views:

552

answers:

3

Is there any way to programatically (in c#) get all the other workstations that are in the same domain/subnetwork as my computer and display some information about them (if they are active, what kind of OS they have installed, their IP etc)?

+4  A: 

nmap does this plus a ton more. Its open source.

Sam Saffron
Thanks, but I have to implement this functionality in an existing program, so I cannot use nmap
melculetz
You can however look at how Nmap does it and reimplement that method in C#...
Moo
nmap's the only solution that will work... Can be called from C# just fine!(Surely .NET programmers can call other programs)
Arafangion
+1  A: 

This is a VB solution, but I'm pretty sure you'll be able to make the changes you need to make this work!

There's probably a better way, but this was a first cut.

Imports System.Net.NetworkInformation
Imports System.Directory Services

Class NetworkInfo

Function GetComputers() as list(Of String)

dim List as new list(of String)

Dim DomainEntry as new DirectoryEntry("WinNT://" + DomainInfo.GetDomain.Trim())
DomainEntry.Children.SchemaFilter.Add("computer")

For Each Machine as DirectoryEntry in DomainEntry.Children
List.Add(Machine.Name)
Next

return List

End Function

End Class

There are all sorts of useful tools knocking about in the System.Net.NetworkInformation namespace to let you capture things like the IP address, etc.

Hooloovoo
Thanks, that was exactly what I needed
melculetz
Does that work for hosts with a static IP address and have not registered a dynamic DNS address?
Arafangion
This works for both static and dynamic IP addresses.
Hooloovoo
How? The only possible way I can think of that this would be true is if _all_ of those machines were part of the domain, which is not necessarily the case.
Arafangion
Read the question - all the machines are on the same domain. This assumes a DNS server is available and contactable.
Hooloovoo
+1  A: 

The simplest approach that I can think of (which is far from fool proof) is to send an ICMP echo request (defined in RFC 792) to 224.0.0.1. C# provides the Ping class to do this. Keep in mind though that you run the risk of dropped packets, there is also the question as to whether or not all the machines on the network support multicast.

Kevin Loney