developer
developer

Reputation: 3871

C#: Get IP Address from Domain Name?

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

Upvotes: 40

Views: 41506

Answers (4)

Andrey
Andrey

Reputation: 60055

You can use the System.Net.Dns class:

Dns.GetHostAddresses("www.test.com");

Upvotes: 40

Babak
Babak

Reputation: 1344

You can get the same results by using:

Dns.GetHostAddresses("yahoo.com");

or

await Dns.GetHostAddressesAsync("yahoo.com");

Upvotes: 2

Pranesh Janarthanan
Pranesh Janarthanan

Reputation: 1194

My answer could be more the same above answers, but here i get the current web app hosted URL / domain name using code and obtained the IP address and from that. I used the code in my C# MVC Web app and its working fine.

Uri myUri = new Uri(((System.Web.HttpContextWrapper)HttpContext).Request.Url.ToString());
var ipAddress = Dns.GetHostAddresses(myUri.Host).FirstOrDefault().ToString();

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

You could use the GetHostAddresses method:

var address = Dns.GetHostAddresses("www.test.com")[0];

Upvotes: 20

Related Questions