获取本机IPV4地址

private static string GetIP()
    {
        string ip = "";
        System.Net.IPAddress[] addressList = System.Net.Dns.GetHostAddresses(System.Net.Dns.GetHostName());
        var tmp = addressList.Where(x => x.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork);
        foreach (var ips in tmp)
        {
            ip = ips.ToString();
        }
        return ip;
    }

该方法需要引用:

using System.Linq;

调用:

string ip = GetIP();

 

posted @ 2017-11-08 16:12  守y1座_空城  阅读(333)  评论(0编辑  收藏  举报