Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1125
  • Last Modified:

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

0
hpops
Asked:
hpops
  • 2
  • 2
1 Solution
 
Todd GerbertIT ConsultantCommented:
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

0
 
hpopsAuthor Commented:
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:
 IPv4 Errors
0
 
vbighamCommented:
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

0
 
Todd GerbertIT ConsultantCommented:
Sorry about that, I'm accustomed to VS 2010/C#-style Linq queries.  So long as you're targeting .Net 3.5 you should be good with this:

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

0
 
hpopsAuthor Commented:
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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now