Vb.net 列出具有IPv4地址的网络适配器名称
我正在尝试列出一台计算机上唯一具有IPv4地址的活动网络适配器。我有这个代码,但它会列出每一个网卡,无论它是否连接Vb.net 列出具有IPv4地址的网络适配器名称,vb.net,networking,ipv4,Vb.net,Networking,Ipv4,我正在尝试列出一台计算机上唯一具有IPv4地址的活动网络适配器。我有这个代码,但它会列出每一个网卡,无论它是否连接 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ListView1.View = View.Details ListView1.GridLines = True ListView1.FullRowS
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ListView1.View = View.Details
ListView1.GridLines = True
ListView1.FullRowSelect = True
ListView1.Columns.Add("Interface Name", 100)
ListView1.Columns.Add("MAC address", 100)
ListView1.Columns.Add("IPv4 address", 100)
ListView1.Columns.Add("Network Mask", 100)
ListView1.Columns.Add("IPv6 Address", 100)
ListView1.Columns.Add("Link Local Address", 100)
ListView1.Columns.Add("IPv5 Address", 100)
End Sub
Private Sub getinterface()
'get all network interface available in system
Dim nics As NetworkInterface() = NetworkInterface.GetAllNetworkInterfaces()
If nics.Length < 0 Or nics Is Nothing Then
MsgBox("No network interfaces found")
Exit Sub
End If
'if interfaces are found let list them. first clear the listview items
ListView1.Items.Clear()
For Each netadapter As NetworkInterface In nics
'next lets set variable to get interface properties for later use
Dim intproperties As IPInterfaceProperties = netadapter.GetIPProperties()
'now add the network adaptername to the list
ListView1.Items.Add(netadapter.Name)
'now get the mac address of this interface
Dim paddress As PhysicalAddress = netadapter.GetPhysicalAddress()
Dim addbyte As Byte() = paddress.GetAddressBytes()
Dim macaddress As String = ""
'now loop through the bytes value and change it to hex
For i = 0 To addbyte.Length - 1
macaddress &= addbyte(i).ToString("X2") 'change string to hex
'now let separate hex value with -except last one
If i <> addbyte.Length - 1 Then
macaddress &= "-"
End If
Next
'ount item in listview
Dim icount As Integer = ListView1.Items.Count
'use try
Try
With ListView1.Items(icount - 1).SubItems
.Add(macaddress)
'.Add(intproperties.UnicastAddresses(2).Address.ToString)
.Add(intproperties.AnycastAddresses(2).Address.ToString)
.Add(intproperties.UnicastAddresses(2).IPv4Mask.ToString)
.Add(intproperties.UnicastAddresses(0).Address.ToString)
.Add(intproperties.UnicastAddresses(1).Address.ToString)
'.Add( IPAddress.Parse(a).AddressFamily == AddressFamily.InterNetwork )
End With
Catch ex As Exception
End Try
Next
'now lets make auto size columns
ListView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent)
End Sub
Private Sub Form1\u Load(ByVal sender作为System.Object,ByVal e作为System.EventArgs)处理MyBase.Load
ListView1.View=View.Details
ListView1.GridLines=True
ListView1.FullRowSelect=True
ListView1.Columns.Add(“接口名称”,100)
ListView1.Columns.Add(“MAC地址”,100)
ListView1.Columns.Add(“IPv4地址”,100)
ListView1.Columns.Add(“网络掩码”,100)
ListView1.Columns.Add(“IPv6地址”,100)
ListView1.Columns.Add(“链接本地地址”,100)
ListView1.Columns.Add(“IPv5地址”,100)
端接头
私有子接口()
'获取系统中所有可用的网络接口
Dim NIC As NetworkInterface()=NetworkInterface.GetAllNetworkInterfaces()
如果nics.Length<0或nics为空,则
MsgBox(“未找到网络接口”)
出口接头
如果结束
'如果找到接口,请列出它们。首先清除listview项
ListView1.Items.Clear()
对于每个netadapter,作为NIC中的网络接口
'下一步让我们设置变量以获取接口属性,供以后使用
Dim intproperties作为IPInterfaceProperties=netadapter.GetIPProperties()
'现在将网络适配器名称添加到列表中
ListView1.Items.Add(netadapter.Name)
'现在获取此接口的mac地址
Dim paddress As PhysicalAddress=netadapter.GetPhysicalAddress()
Dim addbyte As Byte()=paddress.GetAddressBytes()
Dim macaddress As String=“”
'现在循环字节值并将其更改为十六进制
对于i=0到addbyte.Length-1
macaddress&=addbyte(i).ToString(“X2”)将字符串更改为十六进制
'现在让我们用-分隔十六进制值,但最后一个除外
如果我添加byte.Length-1,那么
macaddress&=“-”
如果结束
下一个
'在listview中装入项目
Dim icount As Integer=ListView1.Items.Count
“试试看
尝试
使用ListView1.Items(icount-1).SubItems
.Add(macaddress)
'.Add(intproperties.unicastaddress(2.Address.ToString)
.Add(intproperties.AnycastAddresses(2.Address.ToString)
.Add(intproperties.unicastaddress(2.IPv4Mask.ToString)
.Add(intproperties.UnicastAddresses(0.Address.ToString)
.Add(intproperties.unicastaddress(1.Address.ToString)
'.Add(IPAddress.Parse(a).AddressFamily==AddressFamily.InterNetwork)
以
特例
结束尝试
下一个
'现在让我们自动调整列的大小
ListView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent)
端接头
有更好的方法吗?列出唯一具有IPv4地址的已连接网络适配器。我已经尝试了WMI代码编辑器,但不确定使用哪个编辑器生成适配器名称和IP地址这是我找到的解决方案
For Each netadapter As NetworkInterface In nics
'next lets set variable to get interface properties for later use
Dim intproperties As IPInterfaceProperties = netadapter.GetIPProperties()
'get first number of IP address.
Dim firstnum As String
Try
firstnum = intproperties.UnicastAddresses(1).Address.ToString()
firstnum = firstnum.Substring(0, firstnum.IndexOf("."))
Catch ex As Exception
'If not IPv4 then
firstnum = "NOPE"
End Try
'check if first number if valid IPv4 address
If Val(firstnum) > 0 And Not Val(firstnum) = 169 And Not Val(firstnum) = 127 Then
'now add the network adaptername to the list
ListView1.Items.Add(netadapter.Name)
使用netadapter.OperationalStatus==OperationalStatus.Up选择活动的适配器 (很抱歉,这是C#,但是VB中的等效项应该很容易实现。)