Monday, June 21, 2010

Determine Local Network IP-Address


To readout the IP-addresses the computer is using in the local networks, we first have to include System.Net via using:
using System.Net;
Then we create an instance of the class IPHostEntry, which saves address information for internethosts and set our own computer as host:
IPHostEntry Host = Dns.GetHostEntry(Dns.GetHostName());
In the property AddressList now all available IP-addresses of the host are listed. The local LAN IP then can easily be determined via:
string IPAddress = Host.AddressList[0].ToString();
If there exist multiple network adapters or connections on the cmoputer, the list contains multiple IP-addresses (for example there is nowadays IP6 next to the traditional IP4, a WLAN - connection uses also its own IP - address etc.).
To determine all these IPs, one can simply iterate through the list:

IPHostEntry Host = Dns.GetHostEntry(Dns.GetHostName());
foreach (IPAddress IP in Host.AddressList)
    MessageBox.Show(IP.ToString());

No comments:

Post a Comment