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.
If you wrote the console application you can make it hidden by default.
Create a new console app then then change the "Output Type" type to "Windows Application" (done in the project properties)
If you are using the ProcessStartInfo class you can set the window style to hidden - in the case of console (not GUI) applications, you have to set CreateNoWindow to true:
System.Diagnostics.ProcessStartInfo start =
new System.Diagnostics.ProcessStartInfo();
start.FileName = dir + @"\Myprocesstostart.exe";
start.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; //Hides GUI
start.CreateNoWindow = true; //Hides console
If you are using Process Class then you can write
yourprocess.StartInfo.UseShellExecute = false;
yourprocess.StartInfo.CreateNoWindow = true;
before yourprocess.start(); and process will be hidden
You can use the FreeConsole API to detach the console from the process :
[DllImport("kernel32.dll")]
static extern bool FreeConsole();
(of course this is applicable only if you have access to the console application's source code)
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.
I've got a general solution to share:
using System;
using System.Runtime.InteropServices;
namespace WhateverNamepaceYouAreUsing
{
class Magician
{
[DllImport("kernel32.dll")]
static extern IntPtr GetConsoleWindow();
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
const int HIDE = 0;
const int SHOW = 5;
public static void DisappearConsole()
{
ShowWindow(GetConsoleWindow(), HIDE);
}
}
}
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. ;)
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.