469,904 Members | 2,413 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,904 developers. It's quick & easy.

determining IP Addresses at runtime

How can i determine IP Address of my machine at runtime without
providing any information like HostName?
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #1
11 4861
JR
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.
"Paul Fi" <na*******@hotmail.com> wrote in message
news:ud**************@TK2MSFTNGP12.phx.gbl...
How can i determine IP Address of my machine at runtime without
providing any information like HostName?
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 15 '05 #2
Interestingly enough, this information is totally wrong.

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

"JR" <moo> wrote in message
news:3f**********************@news-text.dial.pipex.com...
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.
"Paul Fi" <na*******@hotmail.com> wrote in message
news:ud**************@TK2MSFTNGP12.phx.gbl...
How can i determine IP Address of my machine at runtime without
providing any information like HostName?
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


Nov 15 '05 #3
Works fine on my machine, gives the same address given by ipconfig.

Austin

On Tue, 6 Jan 2004 00:31:29 +0100, "Thomas Tomiczek [MVP]"
<t.********@thona-consulting.com> wrote:
Interestingly enough, this information is totally wrong.

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

"JR" <moo> wrote in message
news:3f**********************@news-text.dial.pipex.com...
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.
"Paul Fi" <na*******@hotmail.com> wrote in message
news:ud**************@TK2MSFTNGP12.phx.gbl...
> How can i determine IP Address of my machine at runtime without
> providing any information like HostName?
>
>
> *** Sent via Developersdex http://www.developersdex.com ***
> Don't just participate in USENET...get rewarded for it!



Nov 15 '05 #4
Yeah it works thanks guyz!
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #5
"Austin Ehlers" <th***********************@hotmail.com> wrote in
message news:uo********************************@4ax.com
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;
}
--
C# newbie... posts are probably inaccurate, inelegant or both

Nov 15 '05 #6
can u please make a brief explaination on the code and also what object
does "sm" belong to?


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #7
"Paul Fi" <na*******@hotmail.com> wrote in message
news:O1**************@TK2MSFTNGP12.phx.gbl
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

--
C# newbie... posts are probably inaccurate, inelegant or both

Nov 15 '05 #8
do u believe that ur solution is better than the one mentioned b4?if so
in what way?

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #9
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)

"Paul Fi" <na*******@hotmail.com> wrote in message
news:uz**************@TK2MSFTNGP12.phx.gbl...
do u believe that ur solution is better than the one mentioned b4?if so
in what way?

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 15 '05 #10
JR
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.
Reead documentation, guys.

Nov 15 '05 #11
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)

"JR" <moo> wrote in message
news:3f**********************@news-text.dial.pipex.com...
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.
Reead documentation, guys.


Nov 15 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by cpp_weenie | last post: by
5 posts views Thread by John Hardin | last post: by
reply views Thread by John Puopolo | last post: by
9 posts views Thread by Schraalhans Keukenmeester | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.