How to determine if a string is a valid IPv4 or IPv6 address in C #?

I know that regex is dangerous for checking IP addresses because of the various forms that an IP address can take.

I saw similar questions for C and C ++, and they were resolved using a function that does not exist in C # inet_ntop ()

The .NET solutions that I found process only the standard form "ddd.ddd.ddd.ddd". Any suggestions?

+53
c # ip-address
Apr 28 '09 at 17:36
source share
7 answers

You can use this to try to parse it:

IPAddress.TryParse 

Then check AddressFamily , which

Returns System.Net.Sockets.AddressFamily.InterNetwork for IPv4 or System.Net.Sockets.AddressFamily.InterNetworkV6 for IPv6.

EDIT: some sample code. optional:

  string input = "your IP address goes here"; IPAddress address; if (IPAddress.TryParse(input, out address)) { switch (address.AddressFamily) { case System.Net.Sockets.AddressFamily.InterNetwork: // we have IPv4 break; case System.Net.Sockets.AddressFamily.InterNetworkV6: // we have IPv6 break; default: // umm... yeah... I'm going to need to take your red packet and... break; } } 
+103
Apr 28 '09 at 17:40
source share

Just a warning about using System.Net.IpAddress.TryParse() :

If you pass it a string containing an integer (for example, "3"), the TryParse function converts it to "0.0.0.3" and, therefore, a valid InterNetworkV4 address. So, at least the reformatted "0.0.0.3" should be returned to the user application so that the user knows how their input is interpreted.

+25
Apr 01 2018-11-11T00:
source share
 string myIpString = "192.168.2.1"; System.Net.IPAddress ipAddress = null; bool isValidIp = System.Net.IPAddress.TryParse(myIpString, out ipAddress); 

If the value of isValidIp true, you can check ipAddress.AddressFamily to determine if it is IPv4 or IPv6. It has AddressFamily.InterNetwork for IPv4 and AddressFamily.InterNetworkV6 for IPv6.

+12
Apr 28 '09 at 17:45
source share

You can check System.Uri.CheckHostName (value) , which returns Unknown , Dns , IPv4 , IPv6 .

 if( Uri.CheckHostName( value ) != UriHostNameType.Unknown) //then 'value' is a valid IP address or hostname 
+5
Feb 13 '12 at 22:59
source share

If you do not want to parse every integer, but only IP, just check . for IPv4 and : for IPv6.

  if (input.Contains(".") || input.Contains(":")) { IPAddress address; if (IPAddress.TryParse(input, out address)) { switch (address.AddressFamily) { case AddressFamily.InterNetwork: return Ip4Address; case AddressFamily.InterNetworkV6: return Ip6Address; } } } 
+2
Dec 12 '14 at 10:09
source share

You can use the IPAddress.GetAddressBytes () property. Length property

  IPAddress someIP; if (someIP.GetAddressBytes().Length == 4) { // IPV4 } else (someIP.GetAddressBytes().Length == 16) { // IPV6 } else { // Unknown } 

I think it should work

0
Apr 08 '13 at
source share

The combination of tests applied to a string or IPAddress works for me.

  /// <summary> /// Test string for valid ip address format /// </summary> /// /// <param name="Address">The ip address string</param> /// /// <returns>Returns true if address is a valid format</returns> public static bool IsValidIP(IPAddress Ip) { byte[] addBytes = Ip.GetAddressBytes(); switch (Ip.AddressFamily) { case AddressFamily.InterNetwork: if (addBytes.Length == 4) return true; break; case AddressFamily.InterNetworkV6: if (addBytes.Length == 16) return true; break; default: break; } return false; } /// <summary> /// Test string for valid ip address format /// </summary> /// /// <param name="Address">The ip address string</param> /// /// <returns>Returns true if address is a valid format</returns> public static bool IsValidIP(string Address) { IPAddress ip; if (IPAddress.TryParse(Address, out ip)) { switch (ip.AddressFamily) { case System.Net.Sockets.AddressFamily.InterNetwork: if (Address.Length > 6 && Address.Contains(".")) { string[] s = Address.Split('.'); if (s.Length == 4 && s[0].Length > 0 && s[1].Length > 0 && s[2].Length > 0 && s[3].Length > 0) return true; } break; case System.Net.Sockets.AddressFamily.InterNetworkV6: if (Address.Contains(":") && Address.Length > 15) return true; break; default: break; } } return false; } 
0
Sep 21 '15 at 18:26
source share



All Articles