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.

You can check your IP address on myip.help.

23 comments:

  1. This is perfect. Thanks.

    ReplyDelete
  2. 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.

    ReplyDelete
  3. 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.

    ReplyDelete
  4. this is excellent. GoodStuff....

    Thanks,
    Srikanth G

    ReplyDelete
  5. thnks..........

    ReplyDelete
  6. thanks this code is perfect.

    ReplyDelete
  7. shashikant patil7/04/2011 6:36 AM

    thanks this code is perfect

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

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

    ReplyDelete
  9. just not! failsafe - looking for a better solution

    ReplyDelete
  10. Thanks a lot.......
    Its very helpful


    Thanks n regards
    Yashashree

    ReplyDelete
  11. 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]

    ReplyDelete
  12. [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();

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

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

    ReplyDelete
  15. @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.

    ReplyDelete
  16. In vs 2010 Ipaddress in found in length-3

    ReplyDelete
  17. 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

    ReplyDelete
  18. 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.

    ReplyDelete
  19. 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";
    }

    ReplyDelete

Note: Only a member of this blog may post a comment.