本地计算机IP地址有IPv4和IPv6,获取IPv4的方法如下:
1 public IPAddress GetLocalIPv4Address() 2 { 3 IPAddress localIP = null; 4 //获取本机所有的IP地址列表 5 System.Net.IPAddress[] addressList = Dns.GetHostAddresses(Dns.GetHostName()); 6 foreach (IPAddress item in addressList) 7 { 8 //判断是否是IPv4地址 9 if (item.AddressFamily == AddressFamily.InterNetwork) 10 { 11 localIP = item; 12 break; 13 } 14 else 15 continue; 16 17 } 18 return localIP; 19 }