VS2012 C#: Get IPV4 ip address of local machine

I’ve been tasked with writing a dashboard app to display certain information to the senior team at the hospital I work at.

Without distracting you with too much detail,  I’m doing this by writing an app in C# that will allow the user to view statistics by clicking on a notify icon in the system tray.

While this application is running,  the server will push information to it.   In order to register properly as a server, the application has to know its network IP address.

This is usually a simple process programmatically,  but in true Microsoft style,  it’s actually a bit complicated.

I’ll save you time searching through forums and blogs and give you the method I’m using.

Currently (as in “until Microsoft changes something’),  IP addresses are accessed programmatically through the namespace System.Net.Http.   

You will likely have to add this reference to your project.  You can do that by right clicking ‘References’ in your project,  and selecting ‘Add Reference’.   Under Framework,  search for HTTP,  and select System.Net.HTTP.  Make sure you check the one with the right version.


Once this is done, ensure that you make the objects and methods of this reference available to your code by adding using System.Net.Http;

In the code example below,  I’ll show you how to load all your devices IP addresses into an object, and figure out which ones are IPv4.

string strHN = Dns.GetHostName();

IPAddress[] ipEntry = Dns.GetHostAddresses(strHN);
//IPAddress[] addr = ipEntry.AddressList;

int i = 0;

foreach (IPAddress ip in ipEntry)

if (ip.AddressFamily == AddressFamily.InterNetwork)
Console.WriteLine(i + ": IP: " + ip);
Console.WriteLine(i + ": IP Family: " + ip.AddressFamily);



If you need to see all your ip addresses,  simply remove the AddressFamily flow control.

If you need help finding the console window,  this will help.