hpops
asked on
Visual Basic 2008 .NET to display multiple IPv4 addresses
Hello,
I have found some VB .NET code on the Internet capable of returning a single IPv4 address to a TextBox. This works fine.
I have a need to return all active IPv4 addresses for computers like laptops which may have multiple NICs. This code does not seem to be able to return more than 1 IP address. When testing with two active connections this code randomly seems to pick one IP with no rhyme or reason.
Also, I would like to have this working for multiple IPv4 addresses but displayed in a ListBox rather than a TextBox.
This code also will not display my single IP output to a ListBox. I'm not sure why that isn't working.
Any help on both of these issues would be greatly appreciated. Thanks
I have found some VB .NET code on the Internet capable of returning a single IPv4 address to a TextBox. This works fine.
I have a need to return all active IPv4 addresses for computers like laptops which may have multiple NICs. This code does not seem to be able to return more than 1 IP address. When testing with two active connections this code randomly seems to pick one IP with no rhyme or reason.
Also, I would like to have this working for multiple IPv4 addresses but displayed in a ListBox rather than a TextBox.
This code also will not display my single IP output to a ListBox. I'm not sure why that isn't working.
Any help on both of these issues would be greatly appreciated. Thanks
Imports System.Net
Public Class Form1
Function ReturnLocalIP(ByVal Type As Net.Sockets.AddressFamily) As String
'Pass "Net.Sockets.AddressFamily.InterNetwork" for IPv4
'Pass "Net.Sockets.AddressFamily.InterNetworkV6" for IPv6'
Dim HostEntry As System.Net.IPHostEntry = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName)
For Each IP As Net.IPAddress In HostEntry.AddressList
If IP.AddressFamily = Type Then
Return IP.ToString
End If
Next
Return "NULL"
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TextBox1.Text = ReturnLocalIP(Net.Sockets.AddressFamily.InterNetwork)
End Sub
End Class
ASKER
In VS 2008 you may have to add the line extension characters in that LINQ Statements:
Also I think your project will want to reference System.Linq if it does not already.
I believe that the following is also semantically very similar as the long LINQ statement:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim activeIps = From nic In NetworkInterface.GetAllNetworkInterfaces() _
Where nic.OperationalStatus = OperationalStatus.Up And _
nic.NetworkInterfaceType <> NetworkInterfaceType.Loopback _
From ip In nic.GetIPProperties().UnicastAddresses _
Where ip.Address.AddressFamily = AddressFamily.InterNetwork _
Select ip.Address _
For Each ip As IPAddress In activeIps
ActiveIPsListBox.Items.Add(ip.ToString())
Next
End Sub
Also I think your project will want to reference System.Linq if it does not already.
I believe that the following is also semantically very similar as the long LINQ statement:
Dim activeIps As New List(Of IPAddress)
Dim networkInterfaces = NetworkInterface.GetAllNetworkInterfaces()
For Each nic In networkInterfaces
If nic.OperationalStatus = OperationalStatus.Up AndAlso nic.NetworkInterfaceType <> NetworkInterfaceType.Loopback Then
For Each ip In nic.GetIPProperties().UnicastAddresses
If ip.Address.AddressFamily = System.Net.Sockets.AddressFamily.InterNetwork Then
activeIps.Add(ip.Address)
End If
Next
End If
Next
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
tgerbert:
This works perfectly in my form. I can't thank you enough for helping me with this. This is an amazing snippet of code.
Thanks again,
Dave
This works perfectly in my form. I can't thank you enough for helping me with this. This is an amazing snippet of code.
Thanks again,
Dave
Open in new window