|
楼主 |
发表于 2023-2-26 22:02
|
显示全部楼层
''' <summary>
''' 通过NetworkInterface
''' </summary>
''' <returns></returns>
Public Function GetMacByNetworkInterface() As String
Dim key As String = "SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}\"
Dim macAddress As String = String.Empty
Try
Dim nics As NetworkInterface() = NetworkInterface.GetAllNetworkInterfaces()
For Each adapter As NetworkInterface In nics
If adapter.NetworkInterfaceType = NetworkInterfaceType.Ethernet AndAlso adapter.GetPhysicalAddress().ToString().Length <> 0 Then
Dim fRegistryKey As String = key + adapter.Id & Convert.ToString("\Connection")
Dim rk As RegistryKey = Registry.LocalMachine.OpenSubKey(fRegistryKey, False)
If rk IsNot Nothing Then
Dim fPnpInstanceID As String = rk.GetValue("PnpInstanceID", "").ToString()
Dim fMediaSubType As Integer = Convert.ToInt32(rk.GetValue("MediaSubType", 0))
If fPnpInstanceID.Length > 3 AndAlso fPnpInstanceID.Substring(0, 3) = "PCI" Then
macAddress = adapter.GetPhysicalAddress().ToString()
For i As Integer = 1 To 5
macAddress = macAddress.Insert(3 * i - 1, ":")
Next
Exit For
End If
End If
End If
Next
'这里写异常的处理(最好写入日志文件)
Catch ex As Exception
End Try
Return macAddress
End Function |
|