Sabari M Sabari M
Updated date Feb 22, 2021
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.

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

ABOUT THE AUTHOR

Sabari M
Sabari M
Software Professional, India

IT professional with 14+ years of experience in Microsoft Technologies with a strong base in Microsoft .NET (C#.Net, ASP.Net MVC, ASP.NET WEB API, Webservices,...Read More

https://www.techieclues.com/profile/alagu-mano-sabari-m

Comments (0)

There are no comments. Be the first to comment!!!