在VB.NET中,获取网络适配器信息可以使用 NetworkInterface 类的 GetAllNetworkInterfaces 方法。
下面土嘎嘎小编分享一个示例代码,展示如何获取IP适配器信息:
〓〓vb代码如下:〓〓
Imports System.Net.NetworkInformation
Sub GetIPAdapterInfo()
Dim adapters As NetworkInterface() = NetworkInterface.GetAllNetworkInterfaces()
For Each adapter As NetworkInterface In adapters
Console.WriteLine("适配器名称: " & adapter.Name)
Console.WriteLine("描述: " & adapter.Description)
Console.WriteLine("类型: " & adapter.NetworkInterfaceType.ToString())
Console.WriteLine("MAC地址: " & adapter.GetPhysicalAddress().ToString())
'获取IP信息
Dim properties As IPInterfaceProperties = adapter.GetIPProperties()
If properties.UnicastAddresses.Count > 0 Then
Console.WriteLine("IP地址:")
For Each address As UnicastIPAddressInformation In properties.UnicastAddresses
Console.WriteLine("- " & address.Address.ToString())
Next
End If
Console.WriteLine("--------------------------------------")
Next
End Sub
在上面给出的代码中,我们使用 NetworkInterface.GetAllNetworkInterfaces() 方法获取所有网络适配器的信息,并使用 For Each 循环迭代每个适配器。
对于每个适配器,我们打印出名称、描述、类型和MAC地址等基本信息。然后,通过调用 adapter.GetIPProperties() 方法获取IP信息,其中包括该适配器的单播IP地址列表。我们将这些IP地址打印出来。
土粉们可以按照自己的需求修改代码以适应特定情况。注意,此代码需要在引用 System.Net.NetworkInformation 命名空间之后才能正常运行。