I am developing a console application for my public library as a school project. The console application will run as soon as the user logs on and do some background work.
The thing is, I don't want the console application to actually appear. I need it invisible. The last thing I need is complaints because some people got freaked out that a CMD window opened and closed, besides that the library wants it as invisible as possible.
I tried following the code in this thread: http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/ea8b0fd5-a660-46f9-9dcb-d525cc22dcbd
but to no avail, I can still see the console application pop open and close after it has done all of its work.
Is there a better way to stop the console from appearing? Thanks.
If you do not know what I am talking about: press Win+R, type “help” and press ENTER. A black console window will open, execute the HELP command and close again. Often, this is not desired. Instead, the command should execute without any visible window.
"SW_HIDE" hides the window, while "SW_SHOW" shows the window.
Exit a Console Application With the return Method in C# The return statement ends the execution of a method and returns the control to the calling or the main method. We can use the return statement inside the main() function to end our console application's execution.
A console application is a computer program designed to be used via a text-only computer interface, such as a text terminal, the command-line interface of some operating systems (Unix, DOS, etc.) or the text-based interface included with most graphical user interface (GUI) operating systems, such as the Windows Console ...
The best thing to do is just don't compile it as a console application! Compile it as a Windows EXE and no console will show. Then you can just do whatever you need to do in the Main method without showing a UI.
But in any case, if you must hide/show the console window I would steer clear of using FindWindow for this task since there is a much more reliable API for this: GetConsoleWindow. This will give you the HWND of the console window and you can try passing that to ShowWindow.
As Josh Einstein has suggested you can use ShowWindow Api to hide your window.
Here's a example:
using System.Runtime.InteropServices class CommandLine { [DllImport("user32.dll")] static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); [DllImport("Kernel32")] private static extern IntPtr GetConsoleWindow(); const int SW_HIDE=0; const int SW_SHOW=5; static void Main(string[] args) { IntPtr hwnd; hwnd=GetConsoleWindow(); ShowWindow(hwnd,SW_HIDE); //Your logic goes here } }
I am not sure about this code as I have not tested it. Let me know if you face any problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With