c# 获取本机IP 实用代码
1 /// <summary> 2 /// 获取本机IP 3 /// </summary> 4 /// <param name="complete">true 为完整IP false IP段</param> 5 /// <returns></returns> 6 public static string GetIP(bool complete) 7 { 8 try 9 { 10 IPHostEntry IpEntry = Dns.GetHostEntry(Dns.GetHostName()); 11 foreach (IPAddress item in IpEntry.AddressList) 12 { 13 //AddressFamily.InterNetwork ipv4 14 //AddressFamily.InterNetworkV6 ipv6 15 if (item.AddressFamily == AddressFamily.InterNetwork) 16 { 17 if (complete == true) 18 { 19 return item.ToString(); 20 } 21 else 22 { 23 string[] iparr2 = item.ToString().Split('.'); 24 if (iparr2.Length == 4) 25 { 26 return iparr2[0] + "." + iparr2[1] + "." + iparr2[2] + "."; 27 } 28 } 29 } 30 } 31 return ""; 32 } 33 catch { return ""; } 34 }