我有这个函数来获取服务器的IP地址:
public static string GetIpAddressServer()
{
String ip = "";
try
{
string strHostName = System.Net.Dns.GetHostName();
HostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
IPAddress ipAddress = ipHostInfo.AddressList[0];
ip = ipAddress.ToString();
}
catch (Exception ex)
{
ip = "";
}
return ip;
}
但它会返回类似
的内容fe80::3c7f:4fc4:884d:b7f5%11
怎么了?
答案 0 :(得分:1)
如果您想拥有IPv4版本,可以这样做:
var address = Dns.GetHostEntry(strHostname)
.AddressList
.First(ip => ip.AddressFamily == AddressFamily.InterNetwork);