|
Output To Console In Windows Forms Application
This post shows you how to output data to a Console in a Windows Forms Application. I use this technique religiously when developing Windows Forms applications. Debugging is much simpler when you can see information on what your application is doing in a Console. Of course the onus is on you as developer to output data which will help you.
In this post I will use a simple Windows Forms application which adds two numbers.

By default a Windows Forms application does not output to a Console even if you write a Console.Write() or Console.WriteLine(). This shortcoming can be addressed by using Win32 API. In your Windows Forms application you can declare a class which provides a wrapper around Win32 functions.
public class Win32 { /// <summary> /// Allocates a new console for current process. /// </summary> [DllImport("kernel32.dll")] public static extern Boolean AllocConsole(); /// <summary> /// Frees the console. /// </summary> [DllImport("kernel32.dll")] public static extern Boolean FreeConsole(); }
A Console must be started to accept input and display our messages. This can be done by calling Win32.AllocConsole() function. For my example I will start the console in my form’s constructor.
public Form1() { InitializeComponent(); Win32.AllocConsole(); }
Now when I write to Console, I will see data appear in the console window which was started in my constructor.
private void Add(int num1, int num2) { int result = num1 + num2; Console.WriteLine( string.Format("{0} + {1} = {2}", num1, num2, result)); }

As a good practice you should also close the console window by calling Win32.FreeConsole() method before your application exits.
Get Updates By Email
Popular Post
- LINQ To SQL Tutorial
- LINQ To SQL Join On Multiple Conditions
- Code Sample: Programmatically Download File Using C#
- Free Icons And Images With Visual Studio 2008
- Windows 7 Control Panel In Classic Mode
- Dynamic Sort With LINQ
- Use SqlConnection With LINQ To SQL
- StyleCop Tutorial
- Write To Vista Event Log Using C#
- More Details Emerge On Microsoft Master Certification
Tag Cloud
Code Snippets
- Get Current Windows User In C#
- Get Width And Height Of Image In C#
- Get Windows Registry Size With WMI And C#
- Reverse Array Elements Using C#
- Convert Hexadecimal To Number In C#
- Get Free Disk Space Using T-SQL
- SQL Server 2008 – Get All Indexes In A Database
- Get Name Of Current Executing Assembly In C#
- Get CD Or DVD Drive Information Using WMI And C#
- Get Last Row From Table Using LINQ To SQL


September 3rd, 2009 at 10:56 pm
Thanks for the tip. Too bad Console.Clear does not work under Vista although works fine under XP.
September 14th, 2009 at 5:48 am
Thanks, just what I needed
September 14th, 2009 at 9:12 am
Thanks Ed. I’m glad that you found this article helpful.
September 17th, 2009 at 11:33 am
It is great. Very helpful
Thank you.
November 4th, 2009 at 7:59 pm
It worked.
Thanks !
May 27th, 2010 at 9:12 am
Thank you for publishing this solution. I have tried several different offered solution and yours is the only one the worked the very first time. Great job!
May 27th, 2010 at 9:53 am
Bob, Thanks for your motivating comment.