在连接到不同的 VPN 时,我做了一些挖掘工作。到目前为止,以下似乎相当可靠。在许多情况下,依赖 0 或 1 来表示上面建议的实际物理适配器是行不通的。在某些情况下,我实际的以太网适配器是第三个适配器。排除环回、隧道和 ppp 适配器应该可以缩小范围。我发现我的许多非物理适配器都有字符串“00000000000000E0”作为mac地址。
Private Function getMacAddress() As String
Try
Dim adapters As NetworkInterface() = NetworkInterface.GetAllNetworkInterfaces()
Dim adapter As NetworkInterface
Dim myMac As String = String.Empty
For Each adapter In adapters
Select Case adapter.NetworkInterfaceType
'Exclude Tunnels, Loopbacks and PPP
Case NetworkInterfaceType.Tunnel, NetworkInterfaceType.Loopback, NetworkInterfaceType.Ppp
Case Else
If Not adapter.GetPhysicalAddress.ToString = String.Empty And Not adapter.GetPhysicalAddress.ToString = "00000000000000E0" Then
myMac = adapter.GetPhysicalAddress.ToString
Exit For ' Got a mac so exit for
End If
End Select
Next adapter
Return myMac
Catch ex As Exception
Return String.Empty
End Try
End Function