Feed on
Posts
Comments

I’ve had several WPF projects which required showing a log window.  The idea is to log a constant stream of status messages for a admin to monitor.  You could write your own LogWindow and pass messages into the window but there is an easier way.  Tell your WPF application to run as a ‘Console’ application.

Now this sounds funny but it is easy to do and doesn’t impact your WPF application.

  • Right click the Project Properties node in the Solution explorer or choose the Project menu.

image

  • Change the project Application Type to ‘WPF Console Application.

image

Run the application, you will see two windows; the main WPF window and the console window.

image

Using Console

To write to the console window use Console.Write or Console.WriteLine.  You can also control the size and position of the window and change the font color.

Console.SetWindowSize(40, 20);
Console.SetWindowPosition(0, 0);
Console.Title = "Log Window";

Console.ForegroundColor = ConsoleColor.Yellow;

Console.WriteLine("Log: Something happened");

Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Details: Database initialized");

// use worker thread
var del = new Action<string>(Console.WriteLine);
del.Invoke("Details: Database closed");

 

image

6 Responses to “Adding Console Window to WPF Application”

  1. Huw Pendry says:

    In both my copy of Studio 2008 Standard Edition and my works copy which is Professional Edition, there is not a “WPF Console Application” listed, just a “Console Application”, however this still works.

  2. Walt Ritscher says:

    There isn’t a WPF console applcation. You need to create a WPF or Windows Forms application then switch to Console app in the project properties dialog

  3. Angelo Rohit says:

    This was interesting. Thanks.

  4. Huw Pendry says:

    Anyone trying this should be aware that using Console.Readline will tie up the UI thread and make your window unresponsive, unless you do some nifty multi-threading.

  5. Jörg Lenhard says:

    Very easy and very helpfull (and just as valid for Visual Studio Ultimate 2010)! Thank you a lot!

  6. Aybe says:

    Great shortcut !