Win32_NetworkAdapter 查询中的网络地址始终为空

发布于 2024-12-10 22:10:35 字数 1392 浏览 1 评论 0原文

我正在尝试获取活动网络适配器的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夢归不見 2024-12-17 22:10:35

来自微软文档: 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.

挖个坑埋了你 2024-12-17 22:10:35

不存在“真实”IP 地址这样的东西。您发送的每个数据包都会根据适配器的度量和可用路由路径路由到其目的地。您可以通过执行以下操作来猜测哪个 IP 主要用于 Internet 流量:

  1. 按指标对适配器进行排序,最低的在前。
  2. 优先考虑 WAN IP,然后是 192.168.xx,然后是 10.xxx,并完全忽略环回地址。

这将使您猜测“首选”地址,尽管它很难确定。

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:

  1. Order your adapters by metric, lowest first.
  2. Prioritise WAN IPs, then 192.168.x.x, then 10.x.x.x, and totally ignore loopback addresses.

This will give you a guess as to the "preferred" address, though it's hardly definitive.

折戟 2024-12-17 22:10:35

查看 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文