473,511 Members | 17,577 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Getting IP Address of the ACTIVE network card

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
Nov 24 '06 #1
5 11137
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" <fl*******@dsl.pipex.comschreef in bericht
news:%2****************@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

Nov 25 '06 #2
Hi Cor,

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

Best wishes

Paul Bromley

"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:em**************@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" <fl*******@dsl.pipex.comschreef in bericht
news:%2****************@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


Nov 25 '06 #3
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\Para meters\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

Nov 27 '06 #4
Many thanks for this. It should not be a problem for me to convert this to
vb.net. Many thanks

Paul Bromley

"The Frog" <an**************@eu.effem.comwrote in message
news:11**********************@f16g2000cwb.googlegr oups.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\Para meters\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

Nov 27 '06 #5
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:
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
Nov 30 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
4350
by: FrodoBaggins | last post by:
Dear Team, I have set up a Windows Server 2003 on a laptop PC in order to experiment with Visual Studio .NET. On attempting to set up Active Directory, I get the message: "The wizard has...
10
25607
by: Ben Xia | last post by:
Is there some way can detect MAC address with PHP? any help will be appreciate. Ben
4
1830
by: MLH | last post by:
It has been mentioned that a ghosted machine or a machine linked by cable modem and USB may result in my reading a MAC address other than one burned onto a NIC in the machine. That being the case,...
9
1969
by: John J. Hughes II | last post by:
I have a system where my software sits on one server and interacts with another server running MS SQL. My software recieves connects via a socket layer on the internet and does it's thing with the...
3
6040
by: Mark Prenter | last post by:
Hello, I'm trying to find a way to retrieve the MAC address from a network card in a Visual C++ .NET managed application. I've found some examples in C#, but I just can't get them to work in C++....
8
1965
by: pigeonrandle | last post by:
Hi, This started off being a question along the lines of "how do i get the local machine ip address?", which can be done using .... String sIPAddress =...
2
1935
by: Salad | last post by:
If I work on my app on my standalone, things are always fast. If I have an non-split app on the network it runs fast. If I split the app and have both the front end and backend on the network...
6
12782
by: Ryan Liu | last post by:
Hi, If I want to uniquely identify a computer. I can read CPU ID or Mac Address. I heard, but is this true: some BIOS can block CPU ID from being read? (In this case, will I get an exception,...
3
3531
by: tag | last post by:
I'm trying to add an IP address to a pc's network card. In C++ I used the IPHlpApi.lib's function AddIPAddress. Is there a built in way to add an IP Address in C#? Thanks in advance...
0
7245
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7427
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7085
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7512
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5671
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3227
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3214
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1577
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
449
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.