C# Program to Get IP Address

In this article, learn how to get the IP address in C#.


C# Program to Get IP Address

This example shows how to get the IP address from C# in three steps.


using System;
using System.Net;
namespace Program
{
    class IPAddressDemo
    {
        static void Main()
        {
            //Step1: Get the host name
            String strHostName = string.Empty; 
            strHostName = Dns.GetHostName();
            Console.WriteLine("Host name is: " + strHostName);
            
            //Step2: Get IP address by using Host Name.
            IPHostEntry ipHostEntry = Dns.GetHostEntry(strHostName);
            IPAddress[] ipAddr= ipHostEntry .AddressList;
 
            //Step3: Print IP Address
            for (int i = 0; i < ipAddr.Length; i++)
            {
                Console.WriteLine("IP Address {1} : ",ipAddr[i].ToString());
            }
            Console.ReadLine();
        }
    }
}


- Article ends here -

If you have any questions, please feel free to share your questions or comments on the comment box below.

Share this:
We will be happy to hear your thoughts

      Leave a reply

      www.troubleshootyourself.com
      Logo