Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Visual Basic 2008 .NET to display multiple IPv4 addresses

Posted on 2011-03-02
5
Medium Priority
?
1,078 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 35024429
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
ID: 35029540
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
ID: 35029671
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 2000 total points
ID: 35029731
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
ID: 35029769
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

618 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