Reputation: 461
How to get a list of IP Addresses/hostnames in a Network using c#
Upvotes: 2
Views: 1566
Reputation: 234644
I wrote a program that does exactly that. I have it at home :(
It uses the Ping and NetworkInterface classes, to find it through brute force.
I basically iterate through all interfaces, generate all addresses that are valid for the interface network mask, and ping them asynchronously.
It's simple but not perfect. If there are interfaces in the network that don't reply to pings, they won't be listed. This could be the case if there are firewalls active (like Windows Firewall which blocks pings by default).
Oh, yes, it's terribly exhaustive.
EDIT: Even though Mitch Wheat's answer is not what you asked, it reminded me you could use Dns to resolve the hostnames of all those addresses..
Upvotes: 2
Reputation: 41
Something to note however: By default, modern Windows systems do not respond to pings (e.g. XP with the firewall on), so the approach proposed won't work reliably.
The technique I use (albeit not on Windows or C#) is to ping the whole subnet, then look in the ARP cache (as the machines will still respond to ARP requests.)
Not sure how one would accomplish this in C# though.
Upvotes: 1
Reputation: 63485
CodeProject has a pretty crude way of achieving this using the DOS net view
command:
http://www.codeproject.com/KB/IP/ipaddresses.aspx
Upvotes: 0