How to find IP Adress of a computer

Network programming in Windows is facilitated through the use of sockets. In C#, network programming is made simpler through the availability of namespaces such as System.Net and System.Net.Sockets. These namespaces contain classes that provide functionalities similar to the Microsoft WinInet API.

System.Net namespace

The System.Net namespace in C# offers a wide range of classes and methods for working with network-related tasks. One particular aspect of network programming covered by the System.Net namespace is the handling of IP addresses. The classes within this namespace allow you to obtain information about IP addresses, perform IP address resolution, and manipulate IP address-related data.


Using the System.Net namespace, you can retrieve details about IP addresses, such as their version (IPv4 or IPv6), perform conversions between IP address formats, validate IP addresses, and perform other IP address-related operations. This functionality is particularly useful when working with networking applications that require IP address management or manipulation.

Full Source C#
using System; using System.Windows.Forms; using System.Net; namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { try { IPHostEntry hostname = Dns.GetHostByName(textBox1.Text ); IPAddress[] ip = hostname.AddressList; textBox2.Text = ip[0].ToString(); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } } }

If you pass localhost in GetHostByName return the IP Address of local machine .


The System.Net namespace in C# provides a comprehensive set of tools for network programming, including IP address handling, enabling developers to create robust and efficient network-enabled applications.