C#获取本机IPv4地址

string name = Dns.GetHostName();  
                    //IPAddress[] ipadrlist = Dns.GetHostAddresses(name);

//AddressList是一个IPAddress[]类型,这里面存放了系统的所有IP地址,有IPv4的,有IPv6的,还有不同网卡的也会在这里面                 
   IPHostEntry IpEntry = Dns.GetHostEntry(name);
                    string ip = "";
                    for (int i = 0; i < IpEntry.AddressList.Length; i++)
                    {
                        //从IP地址列表中筛选出IPv4类型的IP地址
                        //AddressFamily.InterNetwork表示此IP为IPv4,
                        //AddressFamily.InterNetworkV6表示此地址为IPv6类型
                        if (IpEntry.AddressList[i].AddressFamily == AddressFamily.InterNetwork)
                        {
                            ip= IpEntry.AddressList[i].ToString();
                        }
                    }

你可能感兴趣的:(c#)