Macアドレス

Imports System.Management
Imports System.Net.NetworkInformation
Imports System.Text.RegularExpressions

Module Module1

Sub Main()

' onlineで認証するなら
Dim query As String = "SELECT * FROM Win32_NetworkAdapterConfiguration"
Dim searcher As New ManagementObjectSearcher(query)

Dim queryCollection As ManagementObjectCollection = searcher.Get()
Dim mo As ManagementObject
For Each mo In queryCollection
If Convert.ToBoolean(mo("IPEnabled")) Then
Console.WriteLine(mo("MacAddress").Replace(":", ""))
End If

Next

' offlineで認証するなら
Dim nics As NetworkInterface() = NetworkInterface.GetAllNetworkInterfaces()
For Each nic In nics
If nic.NetworkInterfaceType <> NetworkInterfaceType.Ethernet Then
Continue For
End If

Dim macAddr As String = nic.GetPhysicalAddress().ToString()
Console.WriteLine(macAddr)
Next


' 認証
Dim nics As NetworkInterface() = NetworkInterface.GetAllNetworkInterfaces()
For Each nic In nics
If nic.NetworkInterfaceType <> NetworkInterfaceType.Ethernet Then
Continue For
End If

Dim macAddr As String = nic.GetPhysicalAddress().ToString()
If Regex.IsMatch(macAddr, "^[0-9A-F]{12}$") Then
Console.WriteLine(macAddr)
End If
Next

End Sub

End Module