Search code examples
.netwinformskernel32

Best method to access the Console object in a Windows Forms application?


I am attempting to make my GUI app have a CLI frontend (not the opposite).

I have been able to process arguments without an issue (thanks to System.Environment.GetCommandLineArgs()), but I am curious what the best way to gain access to write output to the console.

I have found reference to AllocConsole(), but it is unclear if this is the only method, as I'd much prefer something revealed in managed code.


Solution

  • There's also AttachConsole but that's always a mistake since it allows output to get intermingled.

    AllocConsole() is it. Just creating a form that acts like a console would be the more Winformy way. With the considerable advantage that the user closing it doesn't cause your program to abort.