c# Getting your machine’s IP addresses.
by brian on Mar.17, 2009, under .NET, c#, c# coding GUI, coding, networking
Here is the simple way to get all of the IP addresses for you machine. This code filters out everything but IPv4 address, but to truly get everything just remove the if statement.
string hostName = Dns.GetHostName();
var addrs = Dns.GetHostAddresses(hostName);
bool hasIP = false;
for (int i = 0; i < addrs.Length; i++)
{
IPAddress addr = addrs[i];
if (addr.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
{
si.DataIP = addr.ToString();
hasIP = true;
break;
}
}
No comments for this entry yet...
