该程序需要获取您的 IP 地址。我用一个函数来做到这一点:
public static string GetLocalIPAddress()
{
var host = Dns.GetHostEntry(Dns.GetHostName());
foreach (var ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
return ip.ToString();
}
}
return null;
}
但是存在以下问题:如果机器上安装了虚拟网络适配器(例如,来自 VirtualBox),则该函数返回其 ip-address。
有没有办法过滤虚拟适配器?
添加对 System.Management 的引用并对Win32_NetworkAdapter类使用 WMI 查询,实际接口应具有PhysicalAdapter=true。要获取接口的 GUID,您可以使用
NetworkInterface.GetAllNetworkInterfaces()
. 像这样的东西: