How to know ip address of your computer using c#

 HOW TO KNOW IP ADDRESS OF YOUR COMPUTER USING C#

NOTE:
  • YOU NEED TO HAVE MICROSOFT VISUAL STUDIO (2010 OR NEWER VERSION) OR YOU CAN HAVE ANY PLATFORM WHICH SUPPORTS C# CONSOLE APPLICATION.

  • AFTER CREATING THE PROJECT, FIRST ADD THE LIBRARIES:
  1. using System.Net;
  2. using System.IO;
  3. using System.Diagnostics;
  4. using System.Threading;

NOW,
  • WRITE THE FOLLOWING LINES IN MAIN METHOD:
 String strHostName = string.Empty; //getting the Host Name.
            strHostName = Dns.GetHostName();
            Console.WriteLine("Local Machine's Host Name: " + strHostName);
            IPHostEntry ipEntry = Dns.GetHostEntry(strHostName);// Using Host Name,IP address is obtained.
            IPAddress[] addr = ipEntry.AddressList;
            for (int i = 0; i < addr.Length; i++)
            {
                Console.WriteLine("IP Address: "+ addr[i].ToString());
            }
            Console.ReadLine();


After Running the program it'll produce the ouput having your Machine Host name and its IP

Comments

Contact Form

Name

Email *

Message *