5

I have a string that can either represent a host name (myip.noip.org, etc.) or it can represent a true address ("127.0.0.1"). What is the best way to resolve this to a System.Net.IPAddress?

Thanks in advance.

3 Answers 3

6

Use the Dns.GetHostAddresses method. This will handle both domain names and raw IP address values

IPAddress[] array = DNs.GetHostAddresses(theString);
Sign up to request clarification or add additional context in comments.

Comments

6

You can call Dns.GetHostEntry on an IP address or a hostname.
It even does reverse lookups for you.

If you don't need reverse lookup, you can call Dns.GetHostAddresses instead.

1 Comment

Dns.Resolve is now listed as obsolete; looks like Dns.GetHostEntry is the direct replacement.
1

Use the IPAddress.Parse method for IP addresses.

IPAddress address = IPAddress.Parse("127.0.0.1");

As mentioned by others, to resolve both IP addresses and host names, use Dns.GetHostEntry which:

Resolves a host name or IP address to an IPHostEntry instance.

IPHostEntry holds a collection of IP addresses in its AddressList property.

2 Comments

No, it won't. Use the Dns class.
OP asked for resolving an IP address or a host name. This only handles ip addresses

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.