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.
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.
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.
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.
Use the Dns.GetHostAddresses
method. This will handle both domain names and raw IP address values
IPAddress[] array = DNs.GetHostAddresses(theString);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With