【发布时间】:2014-06-01 11:23:43
【问题描述】:
public static string GetLocalIpAddress()
{
string hostName = Dns.GetHostName();
IPHostEntry ip = Dns.GetHostEntry(hostName);
string IpAddress = Convert.ToString(ip.AddressList[2]);
return IpAddress.ToString();
}
这有时会显示 Index out of bound 异常 应该是什么问题。? 提前致谢
【问题讨论】:
-
ip.AddressList[2]。您需要检查列表中有多少元素。
-
发生错误是因为数组索引2 > ip.AddressList.Length