c#获取本机IP地址 跟 unity获取本机IP地址
作者:互联网
两种方式,亲测可用
//获取本机ip地址 private string GetIP() { NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces(); foreach (NetworkInterface adater in adapters) { if (adater.Supports(NetworkInterfaceComponent.IPv4)) { UnicastIPAddressInformationCollection UniCast = adater.GetIPProperties().UnicastAddresses; if (UniCast.Count > 0) { foreach (UnicastIPAddressInformation uni in UniCast) { if (uni.Address.AddressFamily == AddressFamily.InterNetwork) { //Debug.Log(uni.Address.ToString()); return uni.Address.ToString(); } } } } } return null; } public static string GetLocalIP() { try { IPHostEntry IpEntry = Dns.GetHostEntry(Dns.GetHostName()); foreach (IPAddress item in IpEntry.AddressList) { //AddressFamily.InterNetwork ipv4 //AddressFamily.InterNetworkV6 ipv6 if (item.AddressFamily == AddressFamily.InterNetwork) { return item.ToString(); } } return ""; } catch { return ""; } }
标签:return,c#,Address,获取,InterNetwork,foreach,IP地址,uni,AddressFamily 来源: https://www.cnblogs.com/qq2351194611/p/11639365.html