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.NetInclude System.Net ReferenceAnd 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 ("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 IPUse 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 ();
但是我用localhost的话得出的是127.0.0.1,怎么样才能得到自己的ip啊?
using System.Net; using System.Net.socket; IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName()); IPAddress ipAddress = ipHostInfo.AddressList[0]; string ip = ipAddress.ToString();
using System.Net;
namespace DNSName
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
static void Main(string[] args)
{
IPHostEntry ipEntry = Dns.GetHostByName ("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 IPUse 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 ();
using System.Net.socket;
IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
IPAddress ipAddress = ipHostInfo.AddressList[0];
string ip = ipAddress.ToString();