C# Program to Get IP Address


This is a C# Program to display the IP address of the machine.

Problem Description

This C# Program Displays the IP Address of the Machine.

Problem Solution

It first obtains the Host Name and gets the IP address with the Host Name.

Program/Source Code

Here is source code of the C# Program to Display the IP Address of the Machine. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

 * C# Program to Display the IP Address of the Machine
using System;
using System.Net;
namespace Program
    class Program
        static void Main()
            //getting the Host Name.
            String strHostName = string.Empty; 
            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 {1} : ",addr[i].ToString());
Program Explanation

In this C# program, the IP address method obtains the selected server IP address information. It then displays the type of address family supported by the server and its IP address in standard and byte format. We are getting the server related information. For loop on the Address List is used to print the type of address family supported by the server.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
Runtime Test Cases
Local Machine's Host Name : win7-pc
IP Address :

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

If you wish to look at all C# Programming examples, go to 1000 C# Programs.

Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.