Link to home
Start Free TrialLog in
Avatar of hpops
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

 
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

Open in new window

Avatar of Todd Gerbert
Todd Gerbert
Flag of United States of America image

This should get all IPv4 addresses on network adapters that are "Up", and are not loopback interfaces.

Imports System.Net.NetworkInformation
Imports System.Net
Imports System.Net.Sockets
Public Class Form1

	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
End Class

Open in new window

Avatar of hpops
hpops

ASKER

Thanks for the speedy reply. Perhaps it's the VB. Net neophyte in me but I can't seem to get this code to work. It has multiple errors. Am I doing something wrong? I added the System.Net reference to my project.

Please see the screenshot for my issues:
 User generated image
In VS 2008 you may have to add the line extension characters in that LINQ Statements:
 
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

Open in new window


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

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Todd Gerbert
Todd Gerbert
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of hpops

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