Getting IP Address of the ACTIVE network card

  • Thread starter Thread starter Paul Bromley
  • Start date Start date
P

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
 
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
 
Hi Cor,

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

Best wishes

Paul Bromley
 
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
 
Many thanks for this. It should not be a problem for me to convert this to
vb.net. Many thanks

Paul Bromley
 
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.
 
Back
Top