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";
}

 

 

  

posted on 2021-03-10 15:47  青叶煮酒  阅读(225)  评论(0编辑  收藏  举报