Subscribe to How-To Geek

Recommended: Click Here to Run a Free Scan for Common PC Errors   [Sponsored Link]

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.

The Geek is the founder of How-To Geek and a geek enthusiast. When he's not coming up with great how-to articles, he's probably writing at his personal blog. This article was written on 11/25/06 and tagged with: Programming, C#

Comments (2)

  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?


Leave a Comment




Leave your friendly comment here. If you have a computer help question, leave it on the forums instead.

Note: Your comment may not show up immediately on the site.

Copyright © 2006-2008 HowToGeek.com. All Rights Reserved.