How to Check Validity of IP Address in .NET


If you are developing a .NET application that needs to validate the IP Addresses at runtime you can use the following code snippet.

First of all import System.Net namespace

using System.Net;

Then use TryParse method of IPAddress class as shown below:

string ipAddress = "20.20.40.50";
IPAddress ipObj = null;

if (IPAddress.TryParse(ipAddress, out ipObj))
{
    Console.WriteLine("IP {0} is valid", ipObj);
}

 

 

About author


User avatar

Waqas Anwar

Founder of EzzyLearning.com, Senior Consultant, Microsoft .NET and Web Technologies

Comments