服务器IP地址 - 返回十六进制数

时间:2015-11-20 10:35:27

标签: c# ip-address

我有这个函数来获取服务器的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

怎么了?

1 个答案:

答案 0 :(得分:1)

如果您想拥有IPv4版本,可以这样做:

 var address = Dns.GetHostEntry(strHostname)
.AddressList
.First(ip => ip.AddressFamily == AddressFamily.InterNetwork);

.Net IPAddress IPv4