简体   繁体   English

NetAddresses在Win32_NetworkAdapter查询中始终为null

[英]NetAddresses always null in Win32_NetworkAdapter query

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. 我试图获取活动网络适配器的IP地址,以便避免当前未连接的虚拟和其他VPN适配器。

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. 在我目前的笔记本电脑上,以下代码返回3个IP V4地址,但我不知道如何从该列表中获取使用中的“真实” IP地址。

        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. 这里查看 MSDN doc时,我认为也许我走对了。 Has anyone obtained the list of IP addresses per adapter successfully? 是否有人成功获取每个适配器的IP地址列表? 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. 我有此代码(感谢SO!),即使计算机连接到网络且IP地址正常运行,string [] netAddresses始终为null。

        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);
                }
            }
        }

from MS docs: http://msdn.microsoft.com/en-us/library/windows/desktop/aa394216%28v=vs.85%29.aspx 来自MS docs: http : //msdn.microsoft.com/zh-CN/library/windows/desktop/aa394216%28v=vs.85%29.aspx

NetworkAddresses NetworkAddresses

Data type: string array Access type: Read-only 数据类型:字符串数组访问类型:只读

Array of network addresses for an adapter. 适配器的网络地址数组。 This property is inherited from CIM_NetworkAdapter. 此属性是从CIM_NetworkAdapter继承的。

This property has not been implemented yet. 此属性尚未实现。 It returns a NULL value by default. 默认情况下,它返回NULL值。

查看WIn32_NetworkAdapterConfiguration,通过使用Win32_NetworkAdapter中的InterfaceIndex,您应该能够使用分配给该nic的ipaddress获得所需的配置。

There's no such thing as a "real" IP address. 没有所谓的“真实” IP地址。 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: 您可以通过执行以下操作来猜测哪个IP主要用于互联网流量:

  1. Order your adapters by metric, lowest first. 按公制顺序订购适配器,最低顺序。
  2. Prioritise WAN IPs, then 192.168.xx, then 10.xxx, and totally ignore loopback addresses. 设置WAN IP的优先级,然后是192.168.xx,然后是10.xxx,并完全忽略环回地址。

This will give you a guess as to the "preferred" address, though it's hardly definitive. 这将使您对“首选”地址进行猜测,尽管很难确定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM