Search code examples
vb.netwmimac-address

Get Specific MAC Address


My VS2015 VB app is reading the MAC address of the users computer. This has worked well except some users are using laptops with docking stations that assign their own MAC address. The code I am using returns the first one it finds, can a specific one be searched for?

    Dim mc As New ManagementClass(New ManagementPath("Win32_Processor"), New ObjectGetOptions(New ManagementNamedValueCollection()))
    Dim moc As ManagementObjectCollection = mc.GetInstances()

    mc.Path = New ManagementPath("Win32_NetworkAdapterConfiguration")

    moc = mc.GetInstances()

    Dim sMac As String = String.Empty

    For Each mo As ManagementObject In moc

        If (mo.GetPropertyValue("IPEnabled") = True) Then

            If (sMac = String.Empty) Then

                sMac = mo.GetPropertyValue("MacAddress").ToString()

            End If

        End If

    Next

Solution

  • This method is using System.Net.NetworkInformation.NetworkInterface instead of directly querying the WMI interface.

    It returns informations on all the current Network Interfaces, except the Loopback interface, where the Operational Status is UP. This usually filters the Teredo and ISATAP interfaces and, of course, all the Network Interfaces that are not currently active.

    The NetworkInterfaceType could also be used to filter other specific interface type, the NetworkInterfaceType.Wireless80211 for example.

    I'm proposing this variant because it's simpler to modify/expand when required.

    Each instance of the NetInterfaceMac class povides:

    • The Interface human-friendly description
    • The IPV4 addresses of the Interface
    • The MAC address if string format ("BF:D1:E8:8C:2B:A4")
    • The MAC address bytes

    Public Class NetInterfaceMac
        Public Property InterfaceDescription() As String
        Public Property IPAddress() As IPAddress
        Public Property MacAddress() As String
        Public Property MacAddressBytes() As Byte()
    End Class
    
    Public Shared Function GetNetworkMACAddresses() As List(Of NetInterfaceMac)
        Dim Macs As New List(Of NetInterfaceMac)()
    
        Dim NetInterfaces As NetworkInterface() = NetworkInterface.GetAllNetworkInterfaces()
    
        Macs.AddRange(NetInterfaces.Where(
            Function(ni) ni.NetworkInterfaceType <> NetworkInterfaceType.Loopback AndAlso
                         ni.OperationalStatus = OperationalStatus.Up).
            Select(Function(ni) New NetInterfaceMac() With {
                .IPAddress = ni.GetIPProperties().UnicastAddresses?.
                     Where(Function(ip) ip.IsDnsEligible = True)?.Select(Function(ip) ip.Address).ToArray(),
                .InterfaceDescription = ni.Description,
                .MacAddress = ni.GetPhysicalAddress().GetAddressBytes().
                    Select(Function(b) b.ToString("X")).Aggregate(Function(s1, s2) s2 + ":" + s1),
                .MacAddressBytes = ni.GetPhysicalAddress().GetAddressBytes()
        }))
    
        Return Macs
    End Function
    

    Sample call:

    Dim Macs As List(Of NetInterfaceMac) = GetNetworkMACAddresses()