VB.NET 获取多张网卡MAC
时间: 2024-10-23 07:05:03 浏览: 19
vb.net获取cpuID 网卡地址 主板id 硬盘id 内存大小 电脑名称信息
4星 · 用户满意度95%
在VB.NET中,获取计算机上所有网络适配器(包括多张网卡)的MAC地址,你可以使用`ManagementObjectSearcher`类配合`.NET`框架中的`ManagementClass`和`ManagementBaseObject`。以下是一个简单的示例:
```vb.net
Imports System.Management
Public Function GetAllNetworkAdapterMacAddresses() As List(Of String)
Dim macAddresses As New List(Of String)
' 创建ManagementObjectSearcher实例,查询所有名为"Win32_NetworkAdapterConfiguration"的对象
Dim query As String = "SELECT MACAddress FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True"
Using searcher As ManagementObjectSearcher = New ManagementObjectSearcher(query)
' 遍历找到的所有适配器
For Each adapter In searcher.Get()
Try
' 获取MAC地址属性值
Dim mac As String = adapter.Properties("MACAddress").Value.ToString()
macAddresses.Add(mac)
Catch ex As Exception
' 捕获并忽略错误
Console.WriteLine($"Error fetching MAC address for {adapter.Name}: {ex.Message}")
End Try
Next
End Using
Return macAddresses
End Function
```
当你调用此函数时,它会返回一个包含所有活动网络适配器MAC地址的列表。
阅读全文