Win32_NetworkAdapter 查询中的网络地址始终为空
我正在尝试获取活动网络适配器的 IP 地址,以便避免当前未连接的虚拟和其他 VPN 适配器。
在我当前的笔记本电脑上,以下代码返回 3 个 IP V4 地址,但我不知道如何从该列表中获取“真实”、正在使用的 IP 地址。
IPAddress[] ipV4Addresses = Array.FindAll(
Dns.GetHostEntry(String.Empty).AddressList,
a => a.AddressFamily == AddressFamily.InterNetwork);
查看 MSDN 文档此处,我想也许我走在正确的道路上。有人成功获取每个适配器的 IP 地址列表吗?如果是这样,请分享您的智慧。谢谢!
此时这都是原型。我有这段代码(感谢SO!),并且 string[] netAddresses 始终为空,即使计算机已连接到网络并且具有正常工作的 IP 地址。
string wmiQuery = "SELECT * FROM Win32_NetworkAdapter WHERE NetConnectionId != NULL";
ManagementObjectSearcher moSearch = new ManagementObjectSearcher(wmiQuery);
ManagementObjectCollection moCollection = moSearch.Get();
foreach (ManagementObject mo in moCollection)
{
Console.WriteLine("{0} is {1}", mo["Name"], mo["NetConnectionStatus"]);
string[] netAddresses = (string[])mo["NetworkAddresses"];
if (netAddresses != null)
{
foreach (string netAddress in netAddresses)
{
Console.WriteLine("\tnet addresses:");
Console.WriteLine("\t\t{0}", netAddress);
}
}
}
I am trying to get the active network adapter's IP address, so that I avoid virtual and other VPN adapters that are not connected at present.
On my current laptop the following code returns 3 IP V4 addresses, and I don't know how to get the "real", in use IP address from that list.
IPAddress[] ipV4Addresses = Array.FindAll(
Dns.GetHostEntry(String.Empty).AddressList,
a => a.AddressFamily == AddressFamily.InterNetwork);
Looking at MSDN doc here, I thought maybe I was on the right track. Has anyone obtained the list of IP addresses per adapter successfully? If so, please share your wisdom. Thanks!
This is all prototype at this point. I have this code (thanks to SO!), and the string[] netAddresses is always null, even with the computer connected to a network and with a functioning IP address.
string wmiQuery = "SELECT * FROM Win32_NetworkAdapter WHERE NetConnectionId != NULL";
ManagementObjectSearcher moSearch = new ManagementObjectSearcher(wmiQuery);
ManagementObjectCollection moCollection = moSearch.Get();
foreach (ManagementObject mo in moCollection)
{
Console.WriteLine("{0} is {1}", mo["Name"], mo["NetConnectionStatus"]);
string[] netAddresses = (string[])mo["NetworkAddresses"];
if (netAddresses != null)
{
foreach (string netAddress in netAddresses)
{
Console.WriteLine("\tnet addresses:");
Console.WriteLine("\t\t{0}", netAddress);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
来自微软文档: http:// /msdn.microsoft.com/en-us/library/windows/desktop/aa394216%28v=vs.85%29.aspx
NetworkAddresses
数据类型:字符串数组
访问类型:只读
适配器的网络地址数组。该属性继承自 CIM_NetworkAdapter。
该属性尚未实现。默认情况下它返回 NULL 值。
from MS docs: http://msdn.microsoft.com/en-us/library/windows/desktop/aa394216%28v=vs.85%29.aspx
NetworkAddresses
Data type: string array
Access type: Read-only
Array of network addresses for an adapter. This property is inherited from CIM_NetworkAdapter.
This property has not been implemented yet. It returns a NULL value by default.
不存在“真实”IP 地址这样的东西。您发送的每个数据包都会根据适配器的度量和可用路由路径路由到其目的地。您可以通过执行以下操作来猜测哪个 IP 主要用于 Internet 流量:
这将使您猜测“首选”地址,尽管它很难确定。
There's no such thing as a "real" IP address. Each packet you send is routed to its destination based on the metric of the adapter and the available routing paths. You can guess which IP is primarily being used for internet traffic by doing the following:
This will give you a guess as to the "preferred" address, though it's hardly definitive.
查看 WIn32_NetworkAdapterConfiguration,通过使用 Win32_NetworkAdapter 中的 InterfaceIndex,您应该能够通过分配给该网卡的 ip 地址获得所需的配置。
Look at WIn32_NetworkAdapterConfiguration, by using the InterfaceIndex from Win32_NetworkAdapter you should be able to get the configuration you need with the ipaddresses assigned to that nic.