TechieClues TechieClues
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.
  • 2.3k
  • 0
  • 0

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

TechieClues
TechieClues

I specialize in creating and sharing insightful content encompassing various programming languages and technologies. My expertise extends to Python, PHP, Java, ... For more detailed information, please check out the user profile

https://www.techieclues.com/profile/techieclues

Comments (0)

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