473,569 Members | 2,716 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).ToS tring()). How can I test for the current active IP
address?? Many thanks in anticipation.

Many thanks

Paul Bromley
Imports System.Net
Public Class CGetLocalIPAddr ess
Dim sIpAddr As String
'Dim myLocalIP As New CGetLocalIPAddr ess
Public Sub New()
GetLocalIPAddre ss()
End Sub
Private Sub GetLocalIPAddre ss()
Dim ipEntry As IPHostEntry = Dns.GetHostByNa me(Environment. MachineName)
Dim mIpAddr As IPAddress() = ipEntry.Address List
Dim i As Integer
sIpAddr = mIpAddr(1).ToSt ring()
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 11145
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.comschree f in bericht
news:%2******** ********@TK2MSF TNGP06.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).ToS tring()). How can I test for the current active
IP address?? Many thanks in anticipation.

Many thanks

Paul Bromley
Imports System.Net
Public Class CGetLocalIPAddr ess
Dim sIpAddr As String
'Dim myLocalIP As New CGetLocalIPAddr ess
Public Sub New()
GetLocalIPAddre ss()
End Sub
Private Sub GetLocalIPAddre ss()
Dim ipEntry As IPHostEntry = Dns.GetHostByNa me(Environment. MachineName)
Dim mIpAddr As IPAddress() = ipEntry.Address List
Dim i As Integer
sIpAddr = mIpAddr(1).ToSt ring()
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******** ******@TK2MSFTN GP06.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.comschree f in bericht
news:%2******** ********@TK2MSF TNGP06.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).ToS tring()). How can I test for the current
active IP address?? Many thanks in anticipation.

Many thanks

Paul Bromley
Imports System.Net
Public Class CGetLocalIPAddr ess
Dim sIpAddr As String
'Dim myLocalIP As New CGetLocalIPAddr ess
Public Sub New()
GetLocalIPAddr ess()
End Sub
Private Sub GetLocalIPAddre ss()
Dim ipEntry As IPHostEntry = Dns.GetHostByNa me(Environment. MachineName)
Dim mIpAddr As IPAddress() = ipEntry.Address List
Dim i As Integer
sIpAddr = mIpAddr(1).ToSt ring()
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 CurrentControlS et, 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("W Script.Shell")

cInterfaceskey =
"HKLM\SYSTEM\Cu rrentControlSet \Services\TCPIP \Parameters\Int erfaces\"
cNICSearch = "HKLM\SOFTWARE\ Microsoft\Windo ws
NT\CurrentVersi on\NetworkCards \1\ServiceName"
' First check which network card to use
cNicServiceName = oSh.RegRead(cNI CSearch)

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

If cIPAddress = "0.0.0.0" Then
cTempIPAddress = oSh.RegRead(cIn terfaceskey + 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.com wrote in message
news:11******** **************@ f16g2000cwb.goo glegroups.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 CurrentControlS et, 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("W Script.Shell")

cInterfaceskey =
"HKLM\SYSTEM\Cu rrentControlSet \Services\TCPIP \Parameters\Int erfaces\"
cNICSearch = "HKLM\SOFTWARE\ Microsoft\Windo ws
NT\CurrentVersi on\NetworkCards \1\ServiceName"
' First check which network card to use
cNicServiceName = oSh.RegRead(cNI CSearch)

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

If cIPAddress = "0.0.0.0" Then
cTempIPAddress = oSh.RegRead(cIn terfaceskey + 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).ToS tring()). 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
4363
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 detected that there are no valid IP addresses assigned to any of the network adapters for this computer. Please verify that the network cable is...
10
25614
by: Ben Xia | last post by:
Is there some way can detect MAC address with PHP? any help will be appreciate. Ben
4
1833
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, I would have a problem. I am using the following code. Blindly, I grabbed it from an old post in this forum. It worked for me on one machine. I...
9
1971
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 SQL. This seems to work fine. The problem is now I have a situation where the internet connect is on another server with firewall. When I...
3
6048
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++. Here's what I've found in C#: using System.Management; ..... ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
8
1972
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 = Dns.Resolve(System.Environment.MachineName).AddressList.ToString unfortunately, the bit reminded me that a computer can have more than one IP (technically, i hadn't forgotten, it had...
2
1939
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 the app's speed drops to a crawl. If I split the app have and make sure the MDBs use file names 8 chars and have a persistent link to the backend...
6
12785
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, null or empty string for method managementObject.Properties?) So maybe Mac address is better way. But when I read Mac address for my laptop, I...
3
3537
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
7694
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
1
7666
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7964
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6278
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5504
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5217
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2107
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 we have to send another system
1
1208
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
936
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.