;

How to Check if a Given IP Address is IPv4 or IPv6 in C#


Tutorialsrack 31/07/2020 C#

In this article, you will learn how to check if a given IP address is IPv4 or IPv6 in C#. There are various ways to check if an IP address is IPv4 or IPv6. 

Here are some examples to check if an IP address is IPv4 or IPv6.

Example 1:  Using IPAddress.TryParse() Method

In this example, we used the IPAddress.TryParse() method to check if the given IP address is IPv4 or IPv6. To use this method, we need to import the namespace:

Add Namespaces
using System.Net;
using System.Net.Sockets;

Here is the source code of the program to check if the given IP address is IPv4 or IPv6.

Example 1:  Using IPAddress.TryParse() Method
using System;
using System.Linq;
using System.Net;
using System.Net.Sockets;

namespace Tutorialsrack
{
    class Program
    {
        /* How to Check if a Given IP Address is IPv4 or IPv6 in C# */
        static void Main(string[] args)
        {
            Check_IP_Version("2001:0db8:85a3:0000:0000:8a2e:0370:7334");
            Check_IP_Version("2001:0db8:85a3:0000:0000:8a2e:0370");
            Check_IP_Version("2001:0db8:85a3:0000:0000:8a2e::5");
            Check_IP_Version("2001:0db8:85a3:0000:0000::2:7335");
            Check_IP_Version("::1");
            Check_IP_Version("::");
            Check_IP_Version("::1234:5678");
            Check_IP_Version("2001:db8::");
            Check_IP_Version("1200:0000:AB00:1234:O000:2552:7777:1313");
            Check_IP_Version("1::5");
            Console.WriteLine("-----------------------------------------------");
            Check_IP_Version("201.52.56.33");
            Check_IP_Version("172.02.02.30");
            Check_IP_Version("127.0.0.1");
            Check_IP_Version("127.0.526.1");
            Check_IP_Version("5");
            Check_IP_Version("0.0.0.5");
            Check_IP_Version("0.0.0.0");
            Check_IP_Version("0.0.5");
            Check_IP_Version("Foo");
            

            //Hit ENTER to exit the program
            Console.ReadKey();
        }

        public static void Check_IP_Version(string IpAddress)
        {
            try
            {
                bool flag = false;
                string IPv = string.Empty;
                IPAddress address;
                if (!string.IsNullOrEmpty(IpAddress))
                {
                    if (IpAddress.Count(c => c=='.') == 3) {
                        flag = IPAddress.TryParse(IpAddress, out address);
                        IPv = "IPv4";
                    }
                    else if(IpAddress.Contains(':'))
                    {
                        if (IPAddress.TryParse(IpAddress, out address))
                        {
                            flag = address.AddressFamily == AddressFamily.InterNetworkV6;
                        }
                        IPv = "IPv6";
                    }
                    else
                    {
                        IPv = "Version of";
                        flag = false;
                    }
                }
                if (flag)
                {
                    Console.WriteLine("{0} is a valid {1} IP address", IpAddress,IPv);
                }
                else
                {
                    Console.WriteLine("{0} is not a valid {1} IP address", IpAddress,IPv);
                }
            }
            catch (Exception) { }
        }
    }
}
Output

2001:0db8:85a3:0000:0000:8a2e:0370:7334 is a valid IPv6 IP address

2001:0db8:85a3:0000:0000:8a2e:0370 is not a valid IPv6 IP address

2001:0db8:85a3:0000:0000:8a2e::5 is a valid IPv6 IP address

2001:0db8:85a3:0000:0000::2:7335 is a valid IPv6 IP address

::1 is a valid IPv6 IP address

:: is a valid IPv6 IP address

::1234:5678 is a valid IPv6 IP address

2001:db8:: is a valid IPv6 IP address

1200:0000:AB00:1234:O000:2552:7777:1313 is not a valid IPv6 IP address

1::5 is a valid IPv6 IP address

-----------------------------------------------

201.52.56.33 is a valid IPv4 IP address

172.02.02.30 is a valid IPv4 IP address

127.0.0.1 is a valid IPv4 IP address

127.0.526.1 is not a valid IPv4 IP address

5 is not a valid Version of IP address

0.0.0.5 is a valid IPv4 IP address

0.0.0.0 is a valid IPv4 IP address

0.0.5 is not a valid Version of IP address

Foo is not a valid Version of IP address

Example 2: Using Regex

In this example, we used the regular expression to check if the version of the IP address is IPv4 or IPv6. To use a regular expression, we need to first import the namespace System.Text.RegularExpressions.

Here is the source code of the program to check if the given IP address is IPv4 or IPv6.

Example 2: Using Regex
using System;
using System.Linq;
using System.Text.RegularExpressions;

namespace Tutorialsrack
{
    class Program
    {
        /* How to Check if a Given IP Address is IPv4 or IPv6 Using Regex Method in C# */
        static void Main(string[] args)
        {
            Check_IP_Version("2001:0db8:85a3:0000:0000:8a2e:0370:7334");
            Check_IP_Version("2001:0db8:85a3:0000:0000:8a2e:0370");
            Check_IP_Version("2001:0db8:85a3:0000:0000:8a2e::5");
            Check_IP_Version("2001:0db8:85a3:0000:0000::2:7335");
            Check_IP_Version("::1");
            Check_IP_Version("::");
            Check_IP_Version("::1234:5678");
            Check_IP_Version("2001:db8::");
            Check_IP_Version("1200:0000:AB00:1234:O000:2552:7777:1313");
            Check_IP_Version("1::5");
            Console.WriteLine("-----------------------------------------------");
            Check_IP_Version("201.52.56.33");
            Check_IP_Version("172.02.02.30");
            Check_IP_Version("127.0.0.1");
            Check_IP_Version("127.0.526.1");
            Check_IP_Version("5");
            Check_IP_Version("0.0.0.5");
            Check_IP_Version("0.0.0.0");
            Check_IP_Version("0.0.5");
            Check_IP_Version("Foo");
            

            //Hit ENTER to exit the program
            Console.ReadKey();
        }

        public static void Check_IP_Version(string IpAddress)
        {
            try
            {
                Regex regex = new Regex(@"((^\s*((([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))\s*$)|(^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$))");
                bool flag = false;
                string IPv = string.Empty;
                if (!string.IsNullOrEmpty(IpAddress))
                {
                    if (IpAddress.Count(c => c=='.') == 3) {
                        flag = regex.IsMatch(IpAddress);
                        IPv = "IPv4";
                    }
                    else if(IpAddress.Contains(':'))
                    {
                        flag = regex.IsMatch(IpAddress);
                        IPv = "IPv6";
                    }
                    else
                    {
                        IPv = "Version of";
                        flag = false;
                    }
                }
                if (flag)
                {
                    Console.WriteLine("{0} is a valid {1} IP address", IpAddress,IPv);
                }
                else
                {
                    Console.WriteLine("{0} is not a valid {1} IP address", IpAddress,IPv);
                }
            }
            catch (Exception) { }
        }
    }
}
Output

2001:0db8:85a3:0000:0000:8a2e:0370:7334 is a valid IPv6 IP address

2001:0db8:85a3:0000:0000:8a2e:0370 is not a valid IPv6 IP address

2001:0db8:85a3:0000:0000:8a2e::5 is a valid IPv6 IP address

2001:0db8:85a3:0000:0000::2:7335 is a valid IPv6 IP address

::1 is a valid IPv6 IP address

:: is a valid IPv6 IP address

::1234:5678 is a valid IPv6 IP address

2001:db8:: is a valid IPv6 IP address

1200:0000:AB00:1234:O000:2552:7777:1313 is not a valid IPv6 IP address

1::5 is a valid IPv6 IP address

-----------------------------------------------

201.52.56.33 is a valid IPv4 IP address

172.02.02.30 is not a valid IPv4 IP address

127.0.0.1 is a valid IPv4 IP address

127.0.526.1 is not a valid IPv4 IP address

5 is not a valid Version of IP address

0.0.0.5 is a valid IPv4 IP address

0.0.0.0 is a valid IPv4 IP address

0.0.5 is not a valid Version of IP address

Foo is not a valid Version of IP address

I hope this article will help you to understand how to check if a given IP address is IPv4 or IPv6 in C#.

Share your valuable feedback, please post your comment at the bottom of this article. Thank you!


Related Posts



Comments

Recent Posts
Tags