Is there a way to hide the console window when executing a console application?
I am currently using a Windows Forms application to start a console process, but I don't want the console window to be displayed while the task is running.
Is there a way to hide the console window when executing a console application?
I am currently using a Windows Forms application to start a console process, but I don't want the console window to be displayed while the task is running.
Just write
I know I'm not answering exactly what you want, but I am wondering if you're asking the right question.
Why don't you use either:
Those sound like better options if all you want is to run a process.
Although as other answers here have said you can change the "Output type" to "Windows Application", please be aware that this will mean that you cannot use
Console.In
as it will become a NullStreamReader.Console.Out
andConsole.Error
seem to still work fine however.If you're creating a program that doesn't require user input you could always just create it as a service. A service won't show any kind of UI.
If you are using the
ProcessStartInfo
class you can set the window style to hidden:I've got a general solution to share:
Just include this class in your project, and call
Magician.DisappearConsole();
.A console will flash when you start the program by clicking on it. When executing from the command prompt, the command prompt disappears very shortly after execution.
I do this for a Discord Bot that runs forever in the background of my computer as an invisible process. It was easier than getting TopShelf to work for me. A couple TopShelf tutorials failed me before I wrote this with some help from code I found elsewhere. ;P
I also tried simply changing the settings in Visual Studio > Project > Properties > Application to launch as a Windows Application instead of a Console Application, and something about my project prevented this from hiding my console - perhaps because DSharpPlus demands to launch a console on startup. I don't know. Whatever the reason, this class allows me to easily kill the console after it pops up.
Hope this Magician helps somebody. ;)