In this code snippet, we will learn how to print the IP address of the computer in C#. If anyone wants to retrieve their computer IP address programmatically then this C# program is the best one to retrieve the IP address details.
using System;
using System.Net;
namespace Test
{
class SampleProgram
{
static void Main()
{
// Get host name
var hostName = Dns.GetHostName();
Console.WriteLine("The hostname of the computer: " + hostName);
IPHostEntry hostEntry = Dns.GetHostEntry(hostName);
IPAddress[] ipAddress = hostEntry.AddressList;
// Get all Ip addresses
for (int i = 0; i < ipAddress.Length; i++)
{
Console.WriteLine("IP Address: " + ipAddress[i]);
}
Console.ReadKey();
}
}
}
Output:
The hostname of the computer: TECHICLUES-PC
IP Address: ab90::fcdc:6b04:66d9:f52%12
IP Address: 192.168.1.42
IP Address: 170.19.69.12
Comments (0)