Friday, January 4, 2008

How To Convert an IP Address To Hostname

You have an IP address that you need to resolve into a hostname.

Use the Dns.GetHostEntry method to get the hostname for an IP address. In the following code, an IP address is resolved, and the hostname is accessible from the HostName property of the IPHostEntry:

 using System;
using System.Net;

//…
// Use the Dns class to resolve the address.
IPHostEntry iphost = Dns.GetHostEntry("127.0.0.1");

// HostName property holds the hostname.
string hostName = iphost.HostName;

// Print out name.
Console.WriteLine(hostName);

No comments:

Post a Comment