tags:

views:

362

answers:

2

How can I get an IP address, given a domain name? For example: www.test.com

+3  A: 
Dns.GetHostAddresses
Andrey
Also note it is PLURAL - and not all of them may work at a given time.
TomTom
True, but i think it rare case that this method returns more then one address, especially in case of internet domains.
Andrey
Andrey: Try `Dns.GetHostAddresses("google.com");`.
Brian Rasmussen
@Brian - six IPs! WOW! so you need to store them all and pick next if one doesn't work.
Andrey
+2  A: 

You could use the GetHostAddresses method:

var address = Dns.GetHostAddresses("www.test.com")[0];
Darin Dimitrov
Will this work in vb.net?
Moshe
no good. what if result set is 0? or more probably more than 1?
Andrey
@Moshe, of course, it is from BCL
Andrey
@Moshe: It should if you have access to the `System.Net.Dns` class.
Svish
@Andrey: If more than one, it would just ignore the rest. If 0, I guess it would crash :p
Svish
@Moshe, in VB.NET : `Dim address = Dns.GetHostAddresses("www.test.com")(0)`
Darin Dimitrov
@Andrey, @Svish - Silly me, but of course. @Darin Dimitrov - Thanks.
Moshe