vb.net 网络编程 - 列出网络接口
vb.net Network Programming - List Network Interfaces
我对 VB.net 有点陌生,正在尝试提取所有网络接口状态并将其显示在列表框中。
我可以提取网络接口卡名称和 MAC 地址并将它们列在前两列中。
但是,在拉取其他数据时出现异常:
Index was out of range. Must be non-negative and less than the size of
the collection. Parameter name: index
有人可以向我解释这里到底发生了什么,并指出正确的方向吗?
我有以下代码:
Imports System.Net.NetworkInformation
Public Class networkInterfaces
Private Sub showNetInts_Click(sender As Object, e As EventArgs) Handles showNetInts.Click
getInterfaces()
End Sub
Private Sub getinterfaces()
Dim nics As NetworkInterface() = NetworkInterface.GetAllNetworkInterfaces()
If nics.Length < 0 Or nics Is Nothing Then
MsgBox("No network interfaces found.")
End If
netInterfaces.Items.Clear()
For Each netadapter As NetworkInterface In nics
'get interface properties
Dim intProperties As IPInterfaceProperties = netadapter.GetIPProperties
netInterfaces.Items.Add(netadapter.Name)
Dim physAddr As PhysicalAddress = netadapter.GetPhysicalAddress
Dim addbyte As Byte() = physAddr.GetAddressBytes
Dim macAddr As String = ""
'loop through bytes value and change value to hex
For i = 0 To addbyte.Length - 1
macAddr &= addbyte(i).ToString("X2") 'changes string to hex
'separate hex value with "-", except last value
If i <> addbyte.Length - 1 Then
macAddr &= "-"
End If
Next
Dim icount As Integer = netInterfaces.Items.Count
With netInterfaces.Items(icount - 1).SubItems
.Add(macAddr)
.Add(intProperties.UnicastAddresses(1).Address.ToString) ' 2 index = ipv4 addr
.Add(intProperties.UnicastAddresses(1).IPv4Mask.ToString) ' gets mask addr
.Add(intProperties.UnicastAddresses(0).Address.ToString) ' 0 index - ipv6 addr
Next
这是我的意思的一个例子:
If so then you just need two nested If blocks, with the outer one
checking whether the Count is greater than 0 and the inner one
checking whether it's greater than 1.
If intProperties.UnicastAddresses.Count > 0 Then
If intProperties.UnicastAddresses.Count > 1 Then
.Add(intProperties.UnicastAddresses(1).Address.ToString) ' 2 index = ipv4 addr
.Add(intProperties.UnicastAddresses(1).IPv4Mask.ToString) ' gets mask addr
End If
.Add(intProperties.UnicastAddresses(0).Address.ToString) ' 0 index - ipv6 addr
End If
我对 VB.net 有点陌生,正在尝试提取所有网络接口状态并将其显示在列表框中。
我可以提取网络接口卡名称和 MAC 地址并将它们列在前两列中。
但是,在拉取其他数据时出现异常:
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
有人可以向我解释这里到底发生了什么,并指出正确的方向吗?
我有以下代码:
Imports System.Net.NetworkInformation
Public Class networkInterfaces
Private Sub showNetInts_Click(sender As Object, e As EventArgs) Handles showNetInts.Click
getInterfaces()
End Sub
Private Sub getinterfaces()
Dim nics As NetworkInterface() = NetworkInterface.GetAllNetworkInterfaces()
If nics.Length < 0 Or nics Is Nothing Then
MsgBox("No network interfaces found.")
End If
netInterfaces.Items.Clear()
For Each netadapter As NetworkInterface In nics
'get interface properties
Dim intProperties As IPInterfaceProperties = netadapter.GetIPProperties
netInterfaces.Items.Add(netadapter.Name)
Dim physAddr As PhysicalAddress = netadapter.GetPhysicalAddress
Dim addbyte As Byte() = physAddr.GetAddressBytes
Dim macAddr As String = ""
'loop through bytes value and change value to hex
For i = 0 To addbyte.Length - 1
macAddr &= addbyte(i).ToString("X2") 'changes string to hex
'separate hex value with "-", except last value
If i <> addbyte.Length - 1 Then
macAddr &= "-"
End If
Next
Dim icount As Integer = netInterfaces.Items.Count
With netInterfaces.Items(icount - 1).SubItems
.Add(macAddr)
.Add(intProperties.UnicastAddresses(1).Address.ToString) ' 2 index = ipv4 addr
.Add(intProperties.UnicastAddresses(1).IPv4Mask.ToString) ' gets mask addr
.Add(intProperties.UnicastAddresses(0).Address.ToString) ' 0 index - ipv6 addr
Next
这是我的意思的一个例子:
If so then you just need two nested If blocks, with the outer one checking whether the Count is greater than 0 and the inner one checking whether it's greater than 1.
If intProperties.UnicastAddresses.Count > 0 Then
If intProperties.UnicastAddresses.Count > 1 Then
.Add(intProperties.UnicastAddresses(1).Address.ToString) ' 2 index = ipv4 addr
.Add(intProperties.UnicastAddresses(1).IPv4Mask.ToString) ' gets mask addr
End If
.Add(intProperties.UnicastAddresses(0).Address.ToString) ' 0 index - ipv6 addr
End If