Home All Groups Group Topic Archive Search About

Getting IP Address of the ACTIVE network card

Author
24 Nov 2006 8:04 PM
Paul Bromley
How do I know which IP address is active??

I have created the following class and this works well, but the first
address it obtains on my machine is not the active one - in fact I do not
know what NIC it refers to the second one in the addresslist is the current
one (mIpAddr(1).ToString()). How can I test for the current active IP
address?? Many thanks in anticipation.

Many thanks

Paul Bromley


Imports System.Net
Public Class CGetLocalIPAddress
Dim sIpAddr As String
'Dim myLocalIP As New CGetLocalIPAddress
Public Sub New()
GetLocalIPAddress()
End Sub
Private Sub GetLocalIPAddress()
Dim ipEntry As IPHostEntry = Dns.GetHostByName(Environment.MachineName)
Dim mIpAddr As IPAddress() = ipEntry.AddressList
Dim i As Integer
sIpAddr = mIpAddr(1).ToString()
End Sub
Public Property LocalIPAddress() As String
Get
Return sIpAddr
End Get
Set(ByVal Value As String)
sIpAddr = Value
End Set
End Property
End Class

Author
25 Nov 2006 9:45 AM
Cor Ligthert [MVP]
Paul,

In my idea are all those IP adresses active.
The IP address locates your computer on the network, it is not a device.

Cor

Show quoteHide quote
"Paul Bromley" <flyfis***@dsl.pipex.com> schreef in bericht
news:%23TzoOPAEHHA.4604@TK2MSFTNGP06.phx.gbl...
> How do I know which IP address is active??
>
> I have created the following class and this works well, but the first
> address it obtains on my machine is not the active one - in fact I do not
> know what NIC it refers to the second one in the addresslist is the
> current one (mIpAddr(1).ToString()). How can I test for the current active
> IP address?? Many thanks in anticipation.
>
> Many thanks
>
> Paul Bromley
>
>
> Imports System.Net
> Public Class CGetLocalIPAddress
> Dim sIpAddr As String
> 'Dim myLocalIP As New CGetLocalIPAddress
> Public Sub New()
> GetLocalIPAddress()
> End Sub
> Private Sub GetLocalIPAddress()
> Dim ipEntry As IPHostEntry = Dns.GetHostByName(Environment.MachineName)
> Dim mIpAddr As IPAddress() = ipEntry.AddressList
> Dim i As Integer
> sIpAddr = mIpAddr(1).ToString()
> End Sub
> Public Property LocalIPAddress() As String
> Get
> Return sIpAddr
> End Get
> Set(ByVal Value As String)
> sIpAddr = Value
> End Set
> End Property
> End Class
>
Author
25 Nov 2006 8:05 PM
Paul Bromley
Hi Cor,

I only have one active IP address. I am not sure where the other one came
from.

Best wishes

Paul Bromley



Show quoteHide quote
"Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in message
news:emRfbYHEHHA.4952@TK2MSFTNGP06.phx.gbl...
> Paul,
>
> In my idea are all those IP adresses active.
> The IP address locates your computer on the network, it is not a device.
>
> Cor
>
> "Paul Bromley" <flyfis***@dsl.pipex.com> schreef in bericht
> news:%23TzoOPAEHHA.4604@TK2MSFTNGP06.phx.gbl...
>> How do I know which IP address is active??
>>
>> I have created the following class and this works well, but the first
>> address it obtains on my machine is not the active one - in fact I do not
>> know what NIC it refers to the second one in the addresslist is the
>> current one (mIpAddr(1).ToString()). How can I test for the current
>> active IP address?? Many thanks in anticipation.
>>
>> Many thanks
>>
>> Paul Bromley
>>
>>
>> Imports System.Net
>> Public Class CGetLocalIPAddress
>> Dim sIpAddr As String
>> 'Dim myLocalIP As New CGetLocalIPAddress
>> Public Sub New()
>> GetLocalIPAddress()
>> End Sub
>> Private Sub GetLocalIPAddress()
>> Dim ipEntry As IPHostEntry = Dns.GetHostByName(Environment.MachineName)
>> Dim mIpAddr As IPAddress() = ipEntry.AddressList
>> Dim i As Integer
>> sIpAddr = mIpAddr(1).ToString()
>> End Sub
>> Public Property LocalIPAddress() As String
>> Get
>> Return sIpAddr
>> End Get
>> Set(ByVal Value As String)
>> sIpAddr = Value
>> End Set
>> End Property
>> End Class
>>
>
>
Author
27 Nov 2006 9:17 AM
The Frog
Hi Paul,

If you want to get the IP address of the active network card, or
possibly cards in a multi NIC machine, the easiest way to know you have
the right information is to get it from the registry directly. The code
below was written for VBA, but the principal is the same. It also takes
into account the possibility of inactive cards, and handles DHCP
assigned addresses as well as fixed.

i hope that this helps you. Sorry I havent the time to re-write it to
..Net.

Cheers

The Frog :)

------------------------------------------------------------------------------------

Function IPDiscover()
' This function retrieves the IP Address from the registry
' It gets it from the CurrentControlSet, so even when using DHCP
' it returns the correct IP Address

' Declare variables

   Dim key
   Dim cTempIPAddress As Variant
   Dim cIPAddress
   Dim cIPAddressKey


   Set oSh = CreateObject("WScript.Shell")

   cInterfaceskey =
"HKLM\SYSTEM\CurrentControlSet\Services\TCPIP\Parameters\Interfaces\"
   cNICSearch = "HKLM\SOFTWARE\Microsoft\Windows
NT\CurrentVersion\NetworkCards\1\ServiceName"


' First check which network card to use
   cNicServiceName = oSh.RegRead(cNICSearch)

' Now read the IP Address from that card
   cIPAddressKey = cInterfaceskey + cNicServiceName + "\IPAddress"
   cTempIPAddress = oSh.RegRead(cIPAddressKey)
   cIPAddress = cTempIPAddress(0)

If cIPAddress = "0.0.0.0" Then
    cTempIPAddress = oSh.RegRead(cInterfaceskey + cNicServiceName +
"\DhcpIPAddress")
    cIPAddress = ""
    cIPAddress = cTempIPAddress
End If

'return the ip address to the function call
   IPDiscover = cIPAddress
End Function
Author
27 Nov 2006 6:08 PM
Paul Bromley
Many thanks for this. It should not be a problem for me to convert this to
vb.net. Many thanks

Paul Bromley



Show quoteHide quote
"The Frog" <andrew.hogend***@eu.effem.com> wrote in message
news:1164619061.606702.222030@f16g2000cwb.googlegroups.com...
> Hi Paul,
>
> If you want to get the IP address of the active network card, or
> possibly cards in a multi NIC machine, the easiest way to know you have
> the right information is to get it from the registry directly. The code
> below was written for VBA, but the principal is the same. It also takes
> into account the possibility of inactive cards, and handles DHCP
> assigned addresses as well as fixed.
>
> i hope that this helps you. Sorry I havent the time to re-write it to
> .Net.
>
> Cheers
>
> The Frog :)
>
> ------------------------------------------------------------------------------------
>
> Function IPDiscover()
> ' This function retrieves the IP Address from the registry
> ' It gets it from the CurrentControlSet, so even when using DHCP
> ' it returns the correct IP Address
>
> ' Declare variables
>
>   Dim key
>   Dim cTempIPAddress As Variant
>   Dim cIPAddress
>   Dim cIPAddressKey
>
>
>   Set oSh = CreateObject("WScript.Shell")
>
>   cInterfaceskey =
> "HKLM\SYSTEM\CurrentControlSet\Services\TCPIP\Parameters\Interfaces\"
>   cNICSearch = "HKLM\SOFTWARE\Microsoft\Windows
> NT\CurrentVersion\NetworkCards\1\ServiceName"
>
>
> ' First check which network card to use
>   cNicServiceName = oSh.RegRead(cNICSearch)
>
> ' Now read the IP Address from that card
>   cIPAddressKey = cInterfaceskey + cNicServiceName + "\IPAddress"
>   cTempIPAddress = oSh.RegRead(cIPAddressKey)
>   cIPAddress = cTempIPAddress(0)
>
> If cIPAddress = "0.0.0.0" Then
>    cTempIPAddress = oSh.RegRead(cInterfaceskey + cNicServiceName +
> "\DhcpIPAddress")
>    cIPAddress = ""
>    cIPAddress = cTempIPAddress
> End If
>
> 'return the ip address to the function call
>   IPDiscover = cIPAddress
> End Function
>
Author
30 Nov 2006 10:50 AM
C-Services Holland b.v.
What IP are you seeing that you're not expecting? 127.0.0.1? If it is,
that's the loopback address, it's always present.

Paul Bromley wrote:

Show quoteHide quote
> How do I know which IP address is active??
>
> I have created the following class and this works well, but the first
> address it obtains on my machine is not the active one - in fact I do not
> know what NIC it refers to the second one in the addresslist is the current
> one (mIpAddr(1).ToString()). How can I test for the current active IP
> address?? Many thanks in anticipation.
>


--
Rinze van Huizen
C-Services Holland b.v