See IP address

B

BobAchgill

How can I see what the IP address of the computer my code is running on
programmatically?

I have tried something someone gave me but it seems to be obsolete (there is
a wavy blue line under the "myip.Address." ) and returns a number that does
not appear to be a IP address. It does not have periods "." in it.

This is what I am using now.


Thanks!

Bob
-----

Dim myip As Net.IPAddress
myip =
Net.Dns.GetHostByName(Environment.MachineName).AddressList(0)

Dim IPAddress As String

IPAddress = myip.Address.ToString
 
J

Jerry Spence1

I have the following (as there may be more than one IP address) on VB2005

Dim IP() As String

Dim MyPC As String = Dns.GetHostName

For n As Integer = 0 To
UBound(Dns.GetHostEntry(Dns.GetHostName()).AddressList)

ReDim Preserve IP(n)

IP(n) = Dns.GetHostEntry(MyPC).AddressList(n).ToString

Next

-Jerry
 
A

Armin Zingler

BobAchgill said:
How can I see what the IP address of the computer my code is running
on programmatically?

I have tried something someone gave me but it seems to be obsolete
(there is a wavy blue line under the "myip.Address." ) and returns a
number that does not appear to be a IP address. It does not have
periods "." in it.

This is what I am using now.


Thanks!

Bob
-----

Dim myip As Net.IPAddress
myip =
Net.Dns.GetHostByName(Environment.MachineName).AddressList(0)

Dim IPAddress As String

IPAddress = myip.Address.ToString


As a computer can have multiple network interfaces and multiple IP
addresses, it is not that simple. First, call

System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces

Then, for each returned interface, call the GetIPProperties function which
returns an IPInterfaceProperties object. Among others, this object has a
UnicastAddresses property returning a UnicastIPAddressInformationCollection
object. It's a collection of UnicastIPAddressInformation objects. They have
an Address property. You can call it's ToString function.


....but why so many words? Some code:

Dim NInterfaces As NetworkInterface()

NInterfaces = NetworkInterface.GetAllNetworkInterfaces

For Each NInterface As NetworkInterface In NInterfaces
Debug.Print("Interface: " & NInterface.Description)

For Each Info As IPAddressInformation _
In NInterface.GetIPProperties().UnicastAddresses

Debug.Print(" " & Info.Address.ToString())
Next
Next


Armin
 
S

ShaneO

BobAchgill said:
How can I see what the IP address of the computer my code is running on
programmatically?
As already mentioned, you need to be aware that there may be more than
one IP address. Therefore, my way is -

Dim NIC_IPs() As IPAddress = Dns.GetHostEntry(Dns.GetHostName()).AddressList

For Each IPAdr As IPAddress In NIC_IPs
Debug.Print(IPAdr.ToString)
Next

(Remember to use 'Imports System.Net')

ShaneO

There are 10 kinds of people - Those who understand Binary and those who
don't.
 
C

cj

This is what I use.

Dim ipAddressInfo As System.Net.IPHostEntry =
System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName())

Dim ipAddress As String = ipAddressInfo.AddressList.GetValue(0).ToString

Works for me.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top