The following code can be used to perform a DNS lookup for the specified host name.
Using DNS will bypass access to the target server. It is an independent distributed directory service that supports a host name to look up IP addresses.
The following code will provide the first returned IP address for the host if a DNS record can be resolved for the provided host name.
public void test() { string hostname = "google.com"; IPAddress ipAdress; if (TryGetIpAddress(hostname, out ipAdress)) { Console.WriteLine("Host:'{0}', IP:{1}.", hostname, ipAdress); } else { Console.WriteLine("Host '{0}' not found.", hostname); } } public bool TryGetIpAddress(string hostname, out IPAddress ipAddress) { const int HostNotFound = 11001; ipAddress = null; try { IPHostEntry hostEntry = Dns.GetHostEntry(hostname); ipAddress = hostEntry.AddressList[0]; } catch (SocketException ex) { if (ex.ErrorCode != HostNotFound) throw; } return (ipAddress != null); }
source share