Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing IP Address in .NET

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.

like image 432
dko Avatar asked Jan 12 '11 17:01

dko


People also ask

What is IP parsing?

The static Parse method creates an IPAddress instance from an IP address expressed in dotted-quad notation for IPv4 and in colon-hexadecimal notation for IPv6.

How do I find my .NET IP address?

Firstly include System.Net. We need to find the name of host to get the IP Address of host. So, the name of host can be retrieved by using the GetHostName() method from the Dns class. By passing the hostname to GetHostByName() method we will get the IP Address.

How do you check if a string is an IP address in C#?

In C#, we can validate a given string is a valid ip address or not by using IPAddress. TryParse method. The below C# function check and returns whether the given string value is valid ip address or not. We can also check AddressFamily of the given ip address.


1 Answers

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

IPAddress[] array = DNs.GetHostAddresses(theString);
like image 172
JaredPar Avatar answered Nov 15 '22 09:11

JaredPar