Tip: Using a Console in a GUI Application

May 11, 2011

4 comments

When working with a WinForms or a WPF application in .NET, a console application is not created by default, so statements involving the Console class normally go to the trash. The console window may be a useful debugging aid, printing anything that may be important during runtime.

Fortunately, there is a way to get it back. Actually, there are two ways. The first, the “hard way” is to create the console explicitly using the native AllocConsole function:

[DllImport("kernel32")]
private static extern void AllocConsole();

All that’s left is to call this function in Main, or the Application class’ constructor.

The other way (the easy way) is to simply go to Project Properties and (in the Application tab) change the output type to a console application. That’s it!

image


To use the console, you can call the usual Console.Write(Line), but there may be a better way, to integrate with the default tracing mechanism of .NET using the Trace class (and friends). The Trace object writes to a collection of TraceListeners, which can be configured easily in the config file.

The following example adds the console as a trace listener:

<configuration>
  <
system.diagnostics
>
    <
trace
>
      <
listeners
>
        <
add name="console" type="System.Diagnostics.ConsoleTraceListener"
/>
      </
listeners
>
    </
trace
>
  </
system.diagnostics
>
</
configuration
>

Now every call to Trace.Write(Line) will also trace to the console (and the default trace listener – the debugger output window – if the debugger is active. Can be captured by the DebugView tool).

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

4 comments

  1. AmirMay 11, 2011 ב 16:29

    What if I want the console to be optional and you would want to use it only for, let’s say, advanced users?
    I stumbled upon this issue before regarding my application and got to the conclusion that the only solution is to use the AllocConsole as you described (based on an argument or a configuration).
    I wish there was an easy trick for an optional console but couldn’t find any.

    You also need to create your project first as a WinForms/WPF in order to have all the prerequisites, init and other stuff required by your original project type. For example: a console project will not add the required references and framework calls required for a WinForms/WPF application.

    Reply
  2. pavelyMay 11, 2011 ב 16:46

    Using AllocConsole is the only way I can think of for this to be optional.
    And yes, you do want to create a WinForms or WPF project first (just to get all the references, etc.). That’s why I showed Project->properties and not File->New Project.

    Reply
  3. MaximMay 11, 2011 ב 18:04

    The method “AllocConsole” attaches Console Window to Running Process, if you’ll close opened console it will kill the process (tested).

    Reply
  4. pavelyMay 11, 2011 ב 19:38

    Sure enough. The console is intended as a debugging aid, nothing else.

    Reply