For the most part, see the answer here . However, I would like to point out the existence of a call to the FreeConsole() API, which allows you to gracefully close the console.
[DllImport("kernel32.dll")] static extern int FreeConsole()
One thing I would like to note: you can see some weird command line appearing before exiting the console if you start from an existing console and attach to it using AttachConsole (unlike AllocConsole ).
This is a temporary problem that is difficult to work with. If this is a problem, install the application as a console application, like the others. This will cause the command line to appear until the application closes, but it may not be the way you want it if you open winform.
In response to your comment : this is either an AttachConsole or an AllocConsole . The example I linked is trying to connect to an existing console first. If this fails (most likely because it does not exist), a new console window is created instead.
If you find a way to get the best of both worlds in terms of command line behavior and interactive GUI mode, please let me know. I havenβt been doing an in-depth search for a solution, but I have a few small applications that will benefit.
By the way, if you plan to use
pipe on the command line (redirecting output to a file, for example), this will not work, as it is, unfortunately.
Thorarin
source share