Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

See IP address

19 views
Skip to first unread message

BobAchgill

unread,
Dec 6, 2007, 12:01:02 PM12/6/07
to
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


Jerry Spence1

unread,
Dec 6, 2007, 12:26:41 PM12/6/07
to
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

"BobAchgill" <BobAc...@discussions.microsoft.com> wrote in message
news:08B42E2E-F0FB-4BC9...@microsoft.com...

Armin Zingler

unread,
Dec 6, 2007, 1:11:34 PM12/6/07
to
"BobAchgill" <BobAc...@discussions.microsoft.com> schrieb


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


ShaneO

unread,
Dec 6, 2007, 2:36:58 PM12/6/07
to
BobAchgill wrote:
> 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.

cj

unread,
Dec 7, 2007, 8:27:43 AM12/7/07
to
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.

0 new messages