在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
1 using System.Net; 2 using System.Net.Sockets; 3 4 /// <summary> 5 /// 获取本机IP地址 6 /// </summary> 7 /// <returns>本机IP地址</returns> 8 public static string GetLocalIP() 9 { 10 try 11 { 12 string HostName = Dns.GetHostName(); //得到主机名 13 IPHostEntry IpEntry = Dns.GetHostEntry(HostName); 14 for (int i = 0; i < IpEntry.AddressList.Length; i++) 15 { 16 //从IP地址列表中筛选出IPv4类型的IP地址 17 //AddressFamily.InterNetwork表示此IP为IPv4, 18 //AddressFamily.InterNetworkV6表示此地址为IPv6类型 19 if (IpEntry.AddressList[i].AddressFamily == AddressFamily.InterNetwork) 20 { 21 return IpEntry.AddressList[i].ToString(); 22 } 23 } 24 return ""; 25 } 26 catch (Exception ex) 27 { 28 return ex.Message; 29 } 30 }
|
请发表评论