Get local IP address with C#
public string GetLocalIPAddress()
{
	foreach (NetworkInterface NetI in NetworkInterface.GetAllNetworkInterfaces())
	{
		if (NetI.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 ||
			NetI.NetworkInterfaceType == NetworkInterfaceType.Ethernet &&
			NetI.OperationalStatus == OperationalStatus.Up)
		{
			foreach (UnicastIPAddressInformation UniIpAddrInfo in NetI.GetIPProperties().UnicastAddresses)
			{
				if (UniIpAddrInfo.Address.AddressFamily != AddressFamily.InterNetwork ||
					UniIpAddrInfo.AddressPreferredLifetime == UInt32.MaxValue)
					continue;
 
				return UniIpAddrInfo.Address.ToString();
			}
		}
	}
 
	return "Unknown";
}
Categories: C# Tags: , , ,
%d bloggers like this: