vb.net 列出网络适配器名称和 IPv4 地址
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26032931/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
List Network Adapter name with IPv4 address
提问by user2364790
Im trying to list the only ACTIVE network adapter with its IPv4 addresses on one computer. i have this code but it will list every network card either its connected or not.
我试图在一台计算机上列出唯一具有 IPv4 地址的 ACTIVE 网络适配器。我有这个代码,但它会列出每个网卡是否已连接。
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
Is there a better way to do this? list the only connected network adapter with IPv4 address. i already try WMI code editor but not sure which one to take for generate adapter name and IP address
有一个更好的方法吗?列出唯一连接的具有 IPv4 地址的网络适配器。我已经尝试过 WMI 代码编辑器,但不确定使用哪一个来生成适配器名称和 IP 地址
回答by Carver Felcut
Here's the solution I found.
这是我找到的解决方案。
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)
回答by JohnT
Use netadapter.OperationalStatus == OperationalStatus.Up to select the adapters that are active.
使用 netadapter.OperationalStatus == OperationalStatus.Up 选择活动的适配器。
(Sorry, that's C#, but the equivalent in VB should be easy.)
(抱歉,那是 C#,但 VB 中的等价物应该很容易。)

