Sunday, June 19, 2011

C# - How to detect all IP addresses from a LAN?

string strHostName = string.Empty;

cmbIPAddress.Items.Clear();

// Getting Ip address of local machine...
// First get the host name of local machine.

strHostName = Dns.GetHostName();

// Then using host name, get the IP address list..

IPHostEntry ipEntry = Dns.GetHostByName(strHostName);
IPAddress[] iparrAddr = ipEntry.AddressList;

if (iparrAddr.Length > 0)
{
for (int intLoop = 0; intLoop < iparrAddr.Length; intLoop++)
cmbIPAddress.Items.Add(iparrAddr[intLoop].ToString());
}

No comments: