使用 VB.NET 2010 获取 MAC 地址
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13025878/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Geting MAC address by using VB.NET 2010
提问by Ali
I have tried to get the MAC addressof each network interface card on a machine by using the below function in VB.NET, but I just realized that this function doesn't work in Windows XP:
我试图通过在VB.NET 中使用以下函数来获取机器上每个网络接口卡的MAC 地址,但我刚刚意识到该函数在 Windows XP 中不起作用:
Function getMacAddress()
Dim nics() As NetworkInterface = NetworkInterface.GetAllNetworkInterfaces()
Return nics(1).GetPhysicalAddress.ToString
End Function
How can I make this code to run on Windows XP? What other alternatives exist to get the list of MAC addresses on Windows XP?
如何使此代码在 Windows XP 上运行?在 Windows XP 上获取 MAC 地址列表还有哪些其他选择?
采纳答案by Mark Hurd
Works for me on XP, except I've got a few interfaces and my first (0th) is my "real" MAC address, and it corresponds to the MAC address reported by a non-.NET program.
在 XP 上对我有用,除了我有几个接口并且我的第一个(第 0 个)是我的“真实”MAC 地址,它对应于非 .NET 程序报告的 MAC 地址。
回答by jhersey29
I did some digging when connecting to different VPNs. So far the below seems pretty reliable. Relying on 0 or 1 for the actual physical adapter as suggested above does not work in many cases. In some cases my actual Ethernet adapter was the 3rd adapter. Excluding the loopbacks, tunnels, and ppp adapters should narrow it down. I found that many of my non physical adapters have the string "00000000000000E0" as the mac address.
我在连接到不同的 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

