Grabbing the Internet Protocol (IP) of a given host name has never been easier, it so easy - it can be done with a single line of code. Anyway, a host name may return one of more Internet Protocol (IP) addresses. I was messing around with some DNS stuff today and I figured I'll share this snippet since there are a bunch of posts out there on how to do DNS queries such as getting the Internet Protocol (IP) address given a host name.

 

    private void GetIPFromHostName(string hostNameOrIPAddress)
{
System.Net.IPAddress[]
ipAddresses = System.Net.Dns.GetHostAddresses(hostNameOrIPAddress);

foreach (System.Net.IPAddress Internet Protocol (IP) in ipAddresses)
{
// Use Internet Protocol (IP) here
}
}
 
To use call:
 
GetIPFromHostName("dotnetclr.com");