【发布时间】:2021-08-20 05:49:39
【问题描述】:
我的机器上安装了 VirtualBox VM,因此出现了一个以太网适配器。我正在通过以下方式枚举我机器的 IP 地址列表:
public string GetLocalIpAddress()
{
try
{
string strHostName = Dns.GetHostName();
// Then using host name, get the IP address list..
IPHostEntry ipEntry = Dns.GetHostEntry(strHostName);
foreach (IPAddress ip in ipEntry.AddressList)
{
if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
{
return string.Format("({0})", ip.ToString());
}
}
}
catch(Exception e)
{
Global.ApplicationLog.AddApplicationLog(EnumAppEventTypes.SYSTEM_ERROR, e.ToString());
}
return "";
}
我的问题是虚拟机的以太网适配器也捕捉到了这种情况:
if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
有没有办法找出我机器的本地 IP 地址而忽略我的虚拟机?
【问题讨论】:
标签: c#