Sunday, 10 April 2016

How to Display the IP Address of the Machine in C# Program


using System;
using System.Net;
namespace Program
{
    class MyMachine
    {
        static void Main()
        {
            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 {1} : ",addr[i].ToString());
            }
            Console.ReadLine();
        }
    }
}
------------------
OUTPUT:
Local Machine's Host Name : win7-pc
IP Address : 192.168.7.9
-----------------------------------------

No comments:

Post a Comment