Solved

Visual Basic 2008 .NET to display multiple IPv4 addresses

Posted on 2011-03-02
5
1,015 Views
Last Modified: 2012-05-11
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
Comment
Question by:hpops
  • 2
  • 2
5 Comments
 
LVL 33

Expert Comment

by:Todd Gerbert
Comment Utility
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
 

Author Comment

by:hpops
Comment Utility
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
 
LVL 7

Expert Comment

by:vbigham
Comment Utility
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
 
LVL 33

Accepted Solution

by:
Todd Gerbert earned 500 total points
Comment Utility
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
 

Author Closing Comment

by:hpops
Comment Utility
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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now