;

How to Check if IPv4 IP Address is Local or Not in C#


Tutorialsrack 31/07/2020 C#

In this article. You will learn how to check if theIPv4  IP address is Local or Not using C#. To get the IP Address and hostname of the local machine, first, you need to add the namespace:

Add namespace
using System.Net;

To Get the Host Name

To get the local hostname or Local computer name, we need to use the static method Dns.GetHostName().

To Get the Host Name
// Retrieve the Name of HOST 
string hostName = Dns.GetHostName(); 

To Get the Local IP Address

To get the local IP address of a local machine, we need to use the static method Dns.GetHostAddresses().

To Get the Local IP Address
// Get the Local IP Address 
IPAddress[] localIPs = Dns.GetHostAddresses(Dns.GetHostName());

Here is the source code of the program to check if the IP address is Local or Not.

How to Check if IPv4 IP Address is Local or Not in C#
using System;
using System.Net;

namespace Tutorialsrack
{
    class Program
    {
        /* How to Check if IP Address is Local or Not in C# */
        static void Main(string[] args)
        {

            //Check IP Address is Local or Not
            Console.WriteLine("IP Address is Local: " + IsLocalIpAddress("localhost"));
            // Output ==> True (Loopback Name)

            Console.WriteLine("IP Address is Local: " + IsLocalIpAddress("127.0.0.1"));
            // Output ==> True (Loopback IP)

            Console.WriteLine("IP Address is Local: " + IsLocalIpAddress("LAPTOP-LH3JFI6M"));
            // Output ==> True (My Computer Name)

            Console.WriteLine("IP Address is Local: " + IsLocalIpAddress("192.168.0.126"));
            // Output ==> True (My Local IP)

            Console.WriteLine("IP Address is Local: " + IsLocalIpAddress("NonExistingHost"));
            // Output ==> False (Non Existing Computer Name)

            Console.WriteLine("IP Address is Local: " + IsLocalIpAddress("201.55.23.1"));
            // Output ==> False (Not Existing IP in my Network)

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

        public static bool IsLocalIpAddress(string host)
        {
            try
            {
                // get host IP addresses
                IPAddress[] hostIPs = Dns.GetHostAddresses(host);
                // get local IP addresses
                IPAddress[] localIPs = Dns.GetHostAddresses(Dns.GetHostName());

                // test if any host IP equals to any local IP or to localhost
                foreach (IPAddress hostIP in hostIPs)
                {
                    // is localhost
                    if (IPAddress.IsLoopback(hostIP)) return true;
                    // is local address
                    foreach (IPAddress localIP in localIPs)
                    {
                        if (hostIP.Equals(localIP)) return true;
                    }
                }
            }
            catch { }
            return false;
        }
    }
}

I hope this article will help you to understand how to check if the IP address is local or not in c#.

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


Related Posts



Comments

Recent Posts
Tags