【笔记】C#获取本机IP地址

| 发布     | 分类 C#  | 标签 C#  Dns 
public static string GetLocalIPAddress()
{
	IPHostEntry host;
	string localIP = "";
	host = Dns.GetHostEntry(Dns.GetHostName());
	foreach (IPAddress ip in host.AddressList)
	{
		if (ip.AddressFamily == AddressFamily.InterNetwork && IsPrivateIP(ip)) // JM: check for all local ranges
		{
			localIP = ip.ToString();
			break;
		}
	}

	return localIP;
}

private static bool IsPrivateIP(IPAddress myIPAddress)
{
	if (myIPAddress.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
	{
		byte[] ipBytes = myIPAddress.GetAddressBytes();

		// 10.0.0.0/24 
		if (ipBytes[0] == 10)
		{
			return true;
		}
		// 172.16.0.0/16
		else if (ipBytes[0] == 172 && ipBytes[1] >= 16 && ipBytes[1] <= 31)
		{
			return true;
		}
		// 192.168.0.0/16
		else if (ipBytes[0] == 192 && ipBytes[1] == 168)
		{
			return true;
		}
	}

	return false;
}
上一篇: 【转载】用DateTime.ToString(string format)输出不同格式的日期
下一篇: 【转载】直接用socket实现HTTP协议(下载专用)