通过WMI确定网络适配器类型

问题描述 投票:10回答:4

我正在使用WMI(Win32_NetworkAdapter)并尝试获取有线或无线连接的物理网络适配器的详细信息,并避免使用虚拟适配器等。

阅读this article它解释了你必须在WMI上做一些聪明的查询以消除虚拟适配器并尝试只返回真正的物理适配器。

阅读this post它解释了你可以比较网络适配器的“描述”中的文本,看它是否包括“无线”,“802.11”或“WLAN”,如果是,那么很可能适配器是无线适配器。

对于今天的.Net版本和其他改进,这些真的是在Windows XP +上确定网络适配器是有线还是无线并且不是来自VM软件等的虚拟适配器的唯一两种方式?如果没有,请解释。

c# .net wmi wmi-query
4个回答
2
投票

我看到这是一个老问题,但我在互联网上有found an answer elsewhere,它描述了如何做到这一点(一直滚动到评论)。

评论者的技术允许识别WiFi和蓝牙接口,其中所有其他类型可以组合在一起。如果目标只是将WiFi与以太网适配器分开,那就足够了。

查询是(Powershell示例):

$nics = Get-WmiObject -Namespace "root/CIMV2" -Query "SELECT * FROM Win32_NetworkAdapter"
$types = Get-WmiObject -Namespace "root/WMI" -Query "SELECT * FROM MSNdis_PhysicalMediumType"

第一个查询是提供适配器列表的常用方法。如前所述,可以通过许多其他选择标准将其过滤为仅包括有效的物理设备。

第二个查询返回一个带有NdisPhysicalMediumType属性的WMI对象,该属性根据链接的站点,其值为9表示WiFi,10表示蓝牙,0表示以太网和大多数其他适配器类型。

看起来加入这两个查询似乎必须使用第一个查询的NameDescription属性和第二个查询的InstanceName属性在脚本中手动完成。


2
投票

您可以在“root \ StandardCimv2”命名空间中使用新的WMI类MSFT_NetAdapter。此类是在Windows 8中引入的。

我们可以使用属性ConnectorPresent仅过滤到物理适配器。接下来我们必须消除Wi-Fi适配器(存在于物理适配器中),我们可以使用InterfaceType和/或NdisPhysicalMedium属性。

InterfaceType由Internet Assigned Names Authority(IANA)定义,并且所有类似以太网的接口都是值ethernetCsmacd(6)(请参阅https://www.iana.org/assignments/ianaiftype-mib/ianaiftype-mib)。

在NdisPhysicalMedium中,以太网适配器的值为0或802.3(14)。

所以我在C#中的解决方案是:

try
{
    var objectSearcher = new ManagementObjectSearcher("root\\StandardCimv2", $@"select Name, InterfaceName, InterfaceType, NdisPhysicalMedium from MSFT_NetAdapter where ConnectorPresent=1"); //Physical adapter

    int count = 0;
    foreach (var managementObject in objectSearcher.Get())
    {
        //The locally unique identifier for the network interface. in InterfaceType_NetluidIndex format. Ex: Ethernet_2.
        string interfaceName = managementObject["InterfaceName"]?.ToString();
        //The interface type as defined by the Internet Assigned Names Authority (IANA).
        //https://www.iana.org/assignments/ianaiftype-mib/ianaiftype-mib
        UInt32 interfaceType = Convert.ToUInt32(managementObject["InterfaceType"]);
        //The types of physical media that the network adapter supports.
        UInt32 ndisPhysicalMedium = Convert.ToUInt32(managementObject["NdisPhysicalMedium"]);

        if (!string.IsNullOrEmpty(interfaceName) &&
            interfaceType == 6 &&       //ethernetCsmacd(6) --for all ethernet-like interfaces, regardless of speed, as per RFC3635
            (ndisPhysicalMedium == 0 || ndisPhysicalMedium == 14))   //802.3
        {
            count++;
        }
    }

    return count;
}
catch (ManagementException)
{
    //Run-time requirements WMI MSFT_NetAdapter class is included in Windows 8 and Windows Server 2012
}

0
投票

也许这会对你有所帮助 http://weblogs.sqlteam.com/mladenp/archive/2010/11/04/find-only-physical-network-adapters-with-wmi-win32_networkadapter-class.aspx使用ManagementObjectSearcher类通过WMI确定网络适配器类型


0
投票
select * from Win32_NetworkAdapter where NetConnectionID LIKE "%Wireless%" or NetConnectionID LIKE "%Wi-Fi%"
© www.soinside.com 2019 - 2024. All rights reserved.