C# 获取本机内网 IPv4 地址

public static string GetLocalIp()
{
    string hostname = Dns.GetHostName();
    IPHostEntry localhost = Dns.GetHostEntry(hostname);
    if (localhost != null)
    {
        foreach (IPAddress item in localhost.AddressList)
        {
            //判断是否是内网IPv4地址
            if (item.AddressFamily == AddressFamily.InterNetwork)
            {
                return item.MapToIPv4().ToString();
            }
        }
    }
    return "127.0.0.1";
}

 

 

  

C# 获取本机内网 IPv4 地址

上一篇:BUPT训练随笔( BUPT '21 WINTER #12: 2017 CCPC OL)


下一篇:Windows10家庭版安装docker