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:
- using System.Net;
- using System.IO;
- using System.Diagnostics;
- 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
After Running the program it'll produce the ouput having your Machine Host name and its IP
Comments
Post a Comment