C# 获取 IPv4

实现代码:

public static string GetLocalIPAddress()
{
    var host = Dns.GetHostEntry(Dns.GetHostName());

    foreach (var ip in host.AddressList)
    {
        if (ip.AddressFamily == AddressFamily.InterNetwork)
        {
            return ip.ToString();
        }
    }

    return "";
}

 

posted @ 2021-09-17 16:25  kueizheng  阅读(75)  评论(0编辑  收藏  举报