Determine Network Adapter Type via WMI

喜你入骨 提交于 2019-12-04 02:59:52

I see this is an old question, but I have found an answer elsewhere on the internet which gives a description of how this can be done (scroll all the way down to the comments).

The comment-er's technique allows the identification of WiFi and Bluetooth interfaces, where all other types may be grouped together. If the goal is only to separate the WiFi from the Ethernet adapters, it should be sufficient.

The queries are (Powershell sample):

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

The first query is the common approach which will provide the list of adapters. As previously noted, it can be filtered to only include valid, physical devices by a number of other selection criteria.

The second query returns a WMI object with a NdisPhysicalMediumType property, which according to the linked site, has the value 9 for WiFi, 10 for Bluetooth, and 0 for Ethernet and most other adapter types.

It looks like joining these two queries has to be done manually in script using the Name or Description property of the first query and the InstanceName property of the second.

You can use new WMI class MSFT_NetAdapter in 'root\StandardCimv2' namespace. This class was introduced in Windows 8.

We can use property ConnectorPresent to filter only to physical adapters. Next we must eliminate Wi-Fi adapters (which is present among physical adapters), we can use InterfaceType and/or NdisPhysicalMedium properties.

InterfaceType is defined by the Internet Assigned Names Authority (IANA) and for all ethernet-like interfaces is value ethernetCsmacd (6) (see https://www.iana.org/assignments/ianaiftype-mib/ianaiftype-mib).

In NdisPhysicalMedium is for ethernet adapters values 0 or 802.3 (14).

So my solution for this in C# is:

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
}
JenniTallia
select * from Win32_NetworkAdapter where NetConnectionID LIKE "%Wireless%" or NetConnectionID LIKE "%Wi-Fi%"
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!