
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 -