Farna
Farna

Reputation: 1176

Find IP Addresses of Local Servers

I would like to retrieve the IP addresses of the server (or servers) on the local network dynamically. How can I retrieve these IP addresses?


Update with code from an answer:

// Query for all the enabled network adapters 
ManagementObjectSearcher objSearcher = new ManagementObjectSearcher(
   "SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled='TRUE'");    
ManagementObjectCollection objCollection = objSearcher.Get();

// Loop through all available network interfaces
foreach (ManagementObject obj in objCollection)
{
   // List all IP addresses of the current network interface
   string[] AddressList = (string[])obj["IPAddress"];
   foreach (string Address in AddressList)
   {
      MessageBox.Show(Address);
   }
}

code source

I use this code but it only returns my own PC's IP address, not all the IPs in network.

Upvotes: 4

Views: 1764

Answers (3)

Spikeh
Spikeh

Reputation: 3705

This could be quite difficult, depending on the configuration of the network. If it's solely a windows network, and the account you're running the application as has Administrator rights, it'll be a bit easier.

The best way would be to query your PDC (Primary Domain Controller). Check out the System.DirectoryServices.ActiveDirectory namespace.

If I remember correctly, you can use LDAP to query the domain controller - as long as the PDC is correctly configured! I found this LDAP query that may help you:

"(&(objectCategory=computer)(|(operatingSystem=Windows Server*)(operatingSystem=Windows 2000 Server))))))"

Of course, that will only query windows 2000 servers - you should be able to modify as needed.

Check out the following links:

http://www.google.co.uk/search?gcx=c&sourceid=chrome&ie=UTF-8&q=c%23+ldap+query

Upvotes: 1

AnthonyLambert
AnthonyLambert

Reputation: 8830

Have you looked at a discovery protocol such as Apple's Bonjour (Zeroconf). http://en.wikipedia.org

Upvotes: 0

Mark Avenius
Mark Avenius

Reputation: 13947

This will let you get the IP address of a machine by name. Is that what you are looking for?

Upvotes: 0

Related Questions