/// <summary>
/// 获取当前使用的IP
/// </summary>
/// <returns></returns>
public static string GetLocalIp()
{
string hostname = Dns.GetHostName();
IPHostEntry localhost = Dns.GetHostEntry(hostname);
IPAddress localaddr = localhost.AddressList[0];
for (int i = 0; i < localhost.AddressList.Length; i++)
{
//从IP地址列表中筛选出IPv4类型的IP地址
//AddressFamily.InterNetwork //IPv4,
//AddressFamily.InterNetworkV6 //IPv6
if (localhost.AddressList[i].AddressFamily == AddressFamily.InterNetwork)
{
return localhost.AddressList[i].ToString();
}
}
return "";
}
12-20
3658
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)