Monday, March 10, 2008

Gets IP Address of Server PC using C# (ASP .NET 1.1)

The function below is used to get the IP Address of Server. The System.Net namespace is needed for this function. This is because IPHostEntry and IPAddress are class in System.Net namespace. Beside this, GetHostName() function also belong to System.Net.Dns.

public string GetIPAddress()
{
   string strHostName = System.Net.Dns.GetHostName();
   IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
   IPAddress ipAddress = ipHostInfo.AddressList[0];

   return ipAddress.ToString();
}