Get Local Computer IP Address using GetHostEntry in C#

If you are running .Net framework 4 and want to get IP address of local machine using C# code then just make following function which will return you local machine ip address.

using System.Net;

private string GetLocalIP()
IPAddress[] IPAddList = Dns.GetHostEntry(Dns.GetHostName()).AddressList;
return IPAddList[IPAddList.Length - 2].ToString();

In previous versions of .net it was [IPAddList.Length – 1] to be return so make change in your code according to your development framework.

Leave a Reply

Your email address will not be published.