25 November 2008

C# How To Get Computer IP Address IPHostEntry

To get the IP address of the local machine in C#,
using system.net
namespace.

From IPHostEntry class, we get the list in a string array.
 using System.Net;

Private string GetIP()
{
string strHostName = "";
strHostName = System.Net.Dns.GetHostName();

IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(strHostName);

IPAddress[] addr = ipEntry.AddressList;

return addr[addr.Length-1].ToString();

}

Read IP address of the local computer.

23 comments:

Chris Nelson said...

This is perfect. Thanks.

smcube said...

Thank you very much...

Naveen said...

There is a slight modification in the code if you want it to work in Visual Studio 2010. Just change the "Length-1" to "Length-2".

Regards,
Naveen.

Anonymous said...

Actually -anything is WRONG. Each address in the list corresponds to the protocol supported. Previously, only IPv4 was supported in windows, so [0], the first element in the array, was the right one. Once IPv6 came out and was running in parallel with IPv4, the function returns two elements in the array, and the first happens to now be the IPv6 address. You can identify which is which by the IP family which is one of the members of the returned item. Just hardcoding an index may work, but it can easily fail; as it did when IPv6 was introduced and coutless programs tried to use [0] and got a six part IPv6 address instead of the four part IPv4.

Anonymous said...

this is excellent. GoodStuff....

Thanks,
Srikanth G

Anonymous said...

thnks..........

Anonymous said...

thank you

Anonymous said...

thanks this code is perfect.

shashikant patil said...

thanks this code is perfect

Raghavendra Nayak said...

This is a Perfect code to get the IP Address of the Current machine.

Thanks a lot.
Regards,
Raaghav
raaghav.nayak86@gmail.com

Anonymous said...

just not! failsafe - looking for a better solution

yashashree said...

Thanks a lot.......
Its very helpful


Thanks n regards
Yashashree

Anonymous said...

jees...listen to smart lads, the guy at 12/10/2010 1:16 PM is right: this code is awfully wrong. You must specify the protocol. In my case this thing returned 4 elements in a list and the correct one for me was (IPv4) at [2]

gucs said...

[Length - 2] does not work if your computer have two network adapter

var strHostName = Dns.GetHostName();
var ipEntry = Dns.GetHostEntry(strHostName);

var addr = ipEntry.AddressList;
var q = from a in addr
where a.AddressFamily == AddressFamily.InterNetwork
select a;
return q.Last().ToString();

SohelElite said...

System.Web.HttpContext.Current.Request.UserHostAddress;

Anonymous said...

why we use q.Last().ToString().why not q.firstordefault. Any specila purpose is there..

Anonymous said...

@Sophie, I think your code snippet attempts to return the IP address of the browsing user, rather than the machine the app is running on.

vivek singh said...

its helpful

Anonymous said...

In vs 2010 Ipaddress in found in length-3

Anonymous said...

In vs 2010 if u r connected to internet than u will get ur IPADDRESS with ipaddr.length-3 and if u not connected than u will get by ipaddr.length-1

Anonymous said...

Thanksss

smcube said...

Unexpectedly I have come back to this blog post after 4 years :). Wow..!

What gucs has proposed is partially ok, but cannot be regarded as a generic solution. In my case, all are ok except the last line where I have to get the first one to get the actual local ipaddress. I have two adapters which are up, and my Local Area Network happenned to be the first one in the list.

var strHostName = Dns.GetHostName();
var ipEntry = Dns.GetHostEntry(strHostName);

var addr = ipEntry.AddressList;
var q = from a in addr
where a.AddressFamily == AddressFamily.InterNetwork
select a;
return q.First().ToString();

System.Net.NetworkInformation might have a solution if wired up with this code of gucs or ASG, to get exactly the intended Ipaddress we would need.

I'll get back if I found a way.

Anonymous said...

private string GetLocalIp()
{
IPHostEntry host;
host = Dns.GetHostEntry(Dns.GetHostName());
foreach (IPAddress ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
return ip.ToString();
}


return "127.0.0.1";
}