You may wish to enable your WinForms application to run from a console window or command line. And when it does, you probably want to send output messages to the console window that launched your WinForms application.
Unfortunately Console.WriteLine()–the standard method of writing to the console window–by default will not work from a WinForms application. That's because the console window that launched your WinForms application belongs to the cmd.exe process, which is separate from your WinForms application process.
So to redirect output from a WinForms application to the console window that launched it, use the AttachConsole Win32 method introduced in Windows XP. AttachConsole attaches the current process to the console window of another process. The special parameter ATTACH_PARENT_PROCESS attaches to the parent process, which in this case is the console window that launched the WinForms application.
Simple Example
Following is a simple WinForms application that redirects its output to the console window that launched it:
using System; using System.Runtime.InteropServices; using System.Windows.Forms; namespace MyWinFormsApp { static class Program { [DllImport( "kernel32.dll" )] static extern bool AttachConsole( int dwProcessId ); private const int ATTACH_PARENT_PROCESS = -1; [STAThread] static void Main( string[] args ) { // redirect console output to parent process; // must be before any calls to Console.WriteLine() AttachConsole( ATTACH_PARENT_PROCESS ); // to demonstrate where the console output is going int argCount = args == null ? 0 : args.Length; Console.WriteLine( "\nYou specified {0} arguments:", argCount ); for (int i = 0; i < argCount; i++) { Console.WriteLine( " {0}", args[i] ); } // launch the WinForms application like normal Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault( false ); Application.Run( new Form1() ); } } }
One Gotcha
There is one problem with this approach. If you redirect the console window output to a text file, for example using the redirect arrow:
MyWinFormsApp.exe >output.txt arg1 arg2
In this case, output will not redirect to the "output.txt" text file as expected, but instead will continue to appear in the console window. Please comment if you have a solution to this issue.
Copyright © 2007-8 Tiwebb Ltd. All rights reserved. This material may not be published, broadcast, rewritten or redistributed without explicit permission from Tiwebb Ltd.

Thanks for the how-to, it really helped me out. I haven't found a solution to your gotcha, but I have another gotcha to tack on.
While this solution successfully redirects output to the console, it doesn't seem to "grab" the console's full attention. That is to say, as soon as my application has loaded, the console drops down and represents the command prompt. Even as output is being printed out from my program to the console, I can navigate folders, run other applications, etc. from the same instance of cmd.exe. For my purposes, this isn't a huge deal, just something I noticed.
Thanks again for the post.
Though the reason is not palpable, but the solution works…
Nicely done….
Thanks so much…
Lalit.
Hello!
I think this try.
Microsoft sux… how can this not be standard??
Really nice =)
Does this change the prompt style if you run it from a cmd.exe window? It might be something else I'm doing, that I only noticed after finding your (very helpful!) tutorial…?