Get IP Address of a Host


Tools Used : Visual C# .NET

The .Net DNS class can be used to get a host name or an IP of a given host name. To use DNS class in your project, you need to include System.Net

Include System.Net Reference

And say I want to get IP address if www.mindcracker.com. The given code will do that for you.

using System;
using System.Net;
namespace DNSName
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
static void Main(string[] args)
{
IPHostEntry ipEntry = Dns.GetHostByName (http://www.mindcracker.com/);
IPAddress [] IpAddr = ipEntry.AddressList;
for (int i = 0; i < IpAddr.Length; i++)
{
Console.WriteLine ("IP Address {0}: {1} ", i, IpAddr[i].ToString ());
}
}
}


Get Local System's host name and IP

Use GetHostName with no parameter to return the host name of a local machine. Once you have host name, pass this host name as a parameter in GetHostByName. See above.

string strHostName = Dns.GetHostName (); 


Similar Articles
Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.