C#为什么我得到127.0.0.1?我怎样才能获得IP地址?

时间:2016-02-21 07:41:50

标签: c# localhost ip-address

localIP =“?”;

try
    {   host = Dns.GetHostEntry(Dns.GetHostName());
        foreach (IPAddress ip in host.AddressList)
        {
            if (ip.AddressFamily.ToString() == "InterNetwork")
            {
                localIP = ip.ToString();
        }
    }
}
 catch (Exception ex)
    { 
       MessageBox.Show(ex.toString());
    }

1 个答案:

答案 0 :(得分:0)

因为它是默认的环回地址。阅读this。除非您的LAN适配器配置了任何其他IP地址,否则这是默认设置。