public string getIp()
{
string ipAddress=null;
try
{
string hostName=Dns.GetHostName();
IPHostEntry ipEntry=Dns.GetHostEntry(hostName);
for(int i=0;i<ipEntry.AddressList.Length;i++)
{
if(ipEntry.AddressList[i].AddressFamily==AddressFamily.InterNetWork)
{
ipAddress=ipEntry.AddressList[i].ToString();
}
}
return ipAddress;
}
catch(Exception ex)
{
return "获取IP出错:"+ex.Message;
}
}
for循环中的判断判断的是ipv4
如果取ipv6则将AddressFamily.InterNetWork替换为AddressFamily.InterNetworkV6