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 = "";
IPAddress ipObj = null;

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



About author

User avatar

Waqas Anwar

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