Parsing an IP address in .NET.

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

Thanks in advance.

+5
source share
3 answers

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

IPAddress[] array = DNs.GetHostAddresses(theString);
+6
source

You can call Dns.GetHostEntryto the IP address or host name.
It even does a reverse search for you.

If you do not need a reverse search, you can call instead Dns.GetHostAddresses.

+6
source

IPAddress.Parse IP-.

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

, IP-, Dns.GetHostEntry, :

IP- IPHostEntry.

IPHostEntrystores a set of IP addresses in a property AddressList.

+1
source

All Articles