determining IP Addresses at runtime

  • Thread starter Thread starter Paul Fi
  • Start date Start date
P

Paul Fi

How can i determine IP Address of my machine at runtime without
providing any information like HostName?
 
Hi,

using System.Net;

IPHostEntry IPAddr = Dns.GetHostByName(Dns.GetHostName());

IPAddr.AddressList is then an array of IP addresses associated with the
local machine.

John.
 
Interestingly enough, this information is totally wrong.

Thomas Tomiczek
THONA Software & Consulting Ltd.
(Microsoft MVP C#/.NET)
 
Austin Ehlers said:
Works fine on my machine, gives the same address given by ipconfig.

I tried that and got very mixed results; it works provided the system is
listed in DNS and the DNS has been updated and unfortunately neither
assumption is reliable especially in the case of my laptop which has an
internal NIC, a PCMCIA NIC, a VPN connection and a NIC in the docking
station.

This appears to be working for me:

using mgt=System.Management;

public System.Collections.ArrayList IPAddresses() {
mgt.PropertyData ipaddr;
mgt.PropertyData gw;
System.Collections.ArrayList IPs=new System.Collections.ArrayList(5);
string sQuery="SELECT IPAddress,DefaultIPGateway from
Win32_NetworkAdapterConfiguration " +
"WHERE IPEnabled=true";
mgt.ManagementObjectSearcher oWMI = new sm.ManagementObjectSearcher(sQuery);
mgt.ManagementObjectCollection oIPs = oWMI.Get();
foreach (sm.ManagementObject mo in oIPs) {
ipaddr = mo.Properties["IPAddress"];
gw=mo.Properties["DefaultIPGateway"];
if (ipaddr.IsLocal && gw.Value!=null) {
if (ipaddr.IsArray) {
string[] sTemp=(string[])ipaddr.Value;
for (int k=0;k<=sTemp.GetUpperBound(0);k++) IPs.Add(sTemp[k]);
}
else
IPs.Add(ipaddr.Value.ToString());
}
}
return IPs;
}
 
can u please make a brief explaination on the code and also what object
does "sm" belong to?
 
Paul Fi said:
can u please make a brief explaination on the code and also what
object does "sm" belong to?

sorry, copy & pasted bits out of longer code and simplified a bit and missed
a change. the 'sm' should be 'mgt'

it uses WMI to get a list of IP address and gateway address for all network
connections; it then checks that the ip address IsLocal and that a gateway
exists (for NICs that are not connected I'm seeing the gateway is null).
what I get back is an arraylist of IP addresses
 
do u believe that ur solution is better than the one mentioned b4?if so
in what way?
 
Simple:

It does not make the stupid assunmption that a ystem that normally is under
external control and may not show your data (the DNS used by your computer)
is actually up to date.

Especially customers using a dial up DNS connection may be surprised.

What the "original solution" (which is a typical "I don't care what reality
says, it works here") returned were NOT the IP addresses that the computer
had.

THe original solution did this:

* Get the name of the computer.
* Ask the DNS name service available for the computer for the IP Addresses
attached to this name.

The second step basically has VERY HIGH failure tolerance, especially in
non-professional environments and ISP scenarios.

The second solution (there is a third, using P/IVOKE to the IpHelper library
and just getting all ip addresses) was using WMI (Windows Management
Instrumentation) to actually ask the local NETWORK CARDS and the TCP IP
STACK bound to them for the addresses registered.

Technically this is a valid solution.

See it like a court case :-)

The second solution was asking a witness what he knows about the case.
Legally valid - the network cards basically have to know what their IP
addresses are).

The first solution would be thrown out as hearsay - basically going out and
asking a stranger what he things about a topi in question. Becuase there is
no requirement that:

* There IS A DNS Server.
* The DNS Server is updated.
* The DNS Server contains the correct information.

and then your software suddenly fails, and basically you told the world you
are not knowing what you are doing. Interesting enough, as I said earlier,
this WILL fail often - basically all dialup internet IP addresses do not get
the host name updates (what the heck does the ISP care about the host name
you tend to enter into your computer?)

"It works here" is for me a reson to fire people. WHen people don#t to their
homwork and read the documentation but then tell ime it works on their
computer - they should server burgers ad McDonalds.

Reead documentation, guys.

When you are asked to return a list of local IP Addresses, do this. Do not
rely on undocumented behavior and assume it will just work.

And yes, btw - I HAVE seen cases (we still try to repro them) where I
actually did get an EXCEPTION on the initial query - with some ISP's at some
times.

Thomas Tomiczek
THONA Software & Consulting Ltd.
(Microsoft MVP C#/.NET)
 
Hello Thomas,

The original solution I posted was in the MSDN documentation, I just
converted it to the same thing in C#. The .NET documentation actually has
examples using the exact same C# code I posted. I have also seen this
solution used in popular open source applications.

Now, I am not an expert Windows programmer I use it only for internal tools
(IOW I never need to worry about it working under all conditions), so I am
not qualified to make any arguments with you about the best way of doing
things. However, anyone who looks in the documentation for a solution to
this problem will almost certainly find this solution first, I did. This has
been reinforced to me many times by seeing other people using the same code.

In any case, Bill's solution is certainly more robust and in my opinion
should be preferred, but it is not something I would've (or have) found just
by browsing through the documentation.

John.
 
Thanks.

Can you please post the exact document URL from the MSDN documentation? I
will have this commented / removed / changed as in error.

Thi should not be in for this question as a possible solution.

(that said: i would prefer they would add a "GetIpAddresses" somewhere).

Thomas Tomiczek
THONA Software & Consulting Ltd.
(Microsoft MVP C#/.NET)
 
Back
Top