编辑:刚刚发现这个......查询本地IP地址 看起来像一个更干净的方式。任你选...
我会采取刺伤... 希望能让你接近。我只有一个适配器连接在我的测试机器上,但我认为(希望)这可以在具有多个适配器的机器上工作。
// Get all profiles
var profiles = NetworkInformation.GetConnectionProfiles();
// filter out profiles that are not currently online
var connected = from p in profiles where p.GetNetworkConnectivityLevel() != NetworkConnectivityLevel.None select p;
// find all hosts
var hosts = NetworkInformation.GetHostNames();
// find hosts that have an IP Address
var online = from h in hosts where h.IPInformation != null select h;
// Now loop there each online connection and match network adapter ids with hosts
foreach (var c in connected)
{
var matches = from o in online where o.IPInformation.NetworkAdapter.NetworkAdapterId == c.NetworkAdapter.NetworkAdapterId select o;
}
这里唯一的问题是,一个物理适配器实际上会显示一次它的 IPv4 地址,一次显示它的 IPv6 地址。您将不得不采取额外的步骤并将它们关联在一起。希望这是您正在寻找的。