C# 获取 ipv4的方法

NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface adapter in nics)
{
    //判断是否为以太网卡
    //Wireless80211         无线网卡    Ppp     宽带连接
    //Ethernet              以太网卡   
    //这里篇幅有限贴几个常用的,其他的返回值大家就自己百度吧!
    if (adapter.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
    {
        //获取以太网卡网络接口信息
        IPInterfaceProperties ip = adapter.GetIPProperties();
        //获取单播地址集
        UnicastIPAddressInformationCollection ipCollection = ip.UnicastAddresses;
        foreach (UnicastIPAddressInformation ipadd in ipCollection)
        {
            //InterNetwork    IPV4地址      InterNetworkV6        IPV6地址
            //Max            MAX 位址
            if (ipadd.Address.AddressFamily == AddressFamily.InterNetwork)
                //判断是否为ipv4
                Console.WriteLine(ipadd.Address.ToString());//获取ip
        }
    }
}

  

你可能感兴趣的:(C# 获取 ipv4的方法)