Answered Display List of IP Addresses

karl_009

Member
Joined
Feb 10, 2010
Messages
6
Programming Experience
Beginner
Hi,

Am looking for away to list the multiple IP addresses a computer can have, either from wireless card, LAN card, and VPN connections ect...

I have been able to do this but it only lists one IP address, is there away to expand this code to list all IP addresses assigned to the computer it is run on?

Am using VB.NET 2008.

Here is the code I have so far;

VB.NET:
Dim myHost As String = System.Net.Dns.GetHostName
Dim myIPs As System.Net.IPHostEntry = System.Net.Dns.GetHostByName(myHost)

        For Each myIP As System.Net.IPAddress In myIPs.AddressList
            ipaddlab.Text = myIP.ToString
            'ipadd1lab.Text = myIP.ToString
        Next

Thanks for any help...
 
Last edited:
Try this:
VB.NET:
ipaddlab.AppendText(myIP.ToString & vbNewline)
 
Hello,

VB 2008 is showing an error, it is;

'AppendText' is not a member of 'System.Windows.Forms.Label'

Can this be stopped?
 
You can stop the Label being Label, but you can also do this:
VB.NET:
ipaddlab.Text &= myIP.ToString
 
Hi,

That did work but the IP Addresses are in a row and not a column so you cannot see them all.

How would you be able to tidy them up?

I have been able to come up with a solution but it is quite static, could the below solution be made less static if the first one cannot be tidied up?


VB.NET:
'Dim ipEntry As IPHostEntry = Dns.GetHostByName(Environment.MachineName)
        'Dim IpAddr As IPAddress() = ipEntry.AddressList
        'Dim i As Integer

        'Me.ipaddlab.Text = IpAddr(0).ToString()
        'Me.ipaddlab1.Text = IpAddr(1).ToString()
        'Me.ipaddlab2.Text = IpAddr(2).ToString()
        'Me.ipaddlab3.Text = IpAddr(3).ToString()

Thanks for your help...
 
What if you append vbNewline to the string also?
 
VB.NET:
        Dim ipEntry As IPHostEntry = Dns.GetHostEntry(Environment.MachineName)
        Dim IpAddr As IPAddress() = ipEntry.AddressList

        'if you want to list every entry
        For Each entry In IpAddr
           TextBox1.Text &= entry.ToString & vbNewLine
        Next

        'if you want to list entries in a range
        For i As Integer = 0 To endrange
            TextBox1.Text &= IpAddr(i).ToString() & vbNewline
            'above line is thesame as:
            'TextBox1.Text =  TextBox1.Text & IpAddr(i).ToString() & vbNewline
        Next
 
Last edited:
I have tryed to use vbNewline but it dosent seem to like it anywhere with in this code;

VB.NET:
ipaddlab.Text &= myIP.ToString
 
You could try the above what I posted. You can try vbNewline or vbCrLf

this should fix it:
TextBox1.Text &= entry.ToString & vbNewLine
 
Thanks to both of you for your help...

The below code is now working...

VB.NET:
        Dim myHost As String = System.Net.Dns.GetHostName
        Dim myIPs As System.Net.IPHostEntry = System.Net.Dns.GetHostByName(myHost)

        For Each myIP As System.Net.IPAddress In myIPs.AddressList
            ipaddlab.Text &= myIP.ToString & vbNewLine
        Next

Thanks
 
Back
Top