Finding the Console Window

Sending debugging and tracing info to the console is a fantastic way to reduce time debugging and tweaking code.

You’ll see it mentioned in almost every thread answering a programming question ever.

However, there are many coders who have difficulty finding where the console window is.

For web pages,  most browsers have debugging tools which can be accessed by hitting the F12 key.  You will see several tabs,  but one of them should be labelled Console.

In .NET,  its really tricky.

In VS2012 at least,  you need to go to the Application tab of the “Properties” page for your project.

You’ll see ‘Output Type’,  and a drop down.  Select ‘Console Application’, and every time you run you’re app,  the console window will appear.

But please, for the love of all that is holy…..remember to switch it back to ‘Windows Application’ for your release build!

Advertisements

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.

references

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);

}

i++;
}

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.