SEARCH

How-To Geek

Get IP Address from DNS Hostname in C#

A frequent task when designing applications that work with TCP/IP and the internet is to lookup an IP address from a hostname. It’s much easier for users to deal with the hostname than having to type in an IP address.

First you’ll add the System.Net namespace to your using section:

using System.Net;

Example of code to get address from hostname:

string howtogeek = "www.howtogeek.com";
IPAddress[] addresslist = Dns.GetHostAddresses(howtogeek);

foreach (IPAddress theaddress in addresslist)
{
   Console.WriteLine(theaddress.ToString());
}

This was tested in C# 2.0.

Lowell Heddings, better known online as the How-To Geek, spends all his free time bringing you fresh geekery on a daily basis. You can follow him on if you'd like.

  • Published 11/25/06

Comments (9)

  1. Robert

    How might I go about getting other DNS records, such as NS records, or MX records in C#?

  2. Ba Than Tin

    How can I know which ip address for which network interface? And if I want IPV4 address only how can I do?

  3. vinh

    thanks for your info.

  4. biosys

    thanks for info :) works perfect!

  5. Narendra

    how can i print all the systems address connected in a network through window application in c# vs 2008

  6. arnr

    hi
    can you help me to find ip address form dns in visualscript for computers in active directory
    please

  7. vinod

    following code returning interaces of local computer but i want inerfaces of the other computer based on ip address ? any one can help me

    NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
    foreach (NetworkInterface ni in nics)
    {
    if (ni.OperationalStatus == OperationalStatus.Up)
    {

    IPAddressCollection ips = ni.GetIPProperties().DnsAddresses;

    foreach (System.Net.IPAddress ip in ips)
    {
    strDnsHostName = ip.ToString();
    break;
    }
    }
    }

  8. HELNE BALRAJ

    where can we get an Ip address for our wireless security system so that it can be monitored remotely.

  9. Dave

    Good morning.

    When I use the Get IP Address from DNS Hostname in C# code, it does pull in the IP Address: 208.43.115.82, but when I step through the code, I am getting an AddressList[0].ScopeId threw an exception of type System.Net.Sockets.SocketException.

    I am using VS 2008, C#, and 3.5. Any suggestions? WHEELS

Enter Your Email Here to Get Access for Free:

Go check your email!