22

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.

Kratz
  • 487
  • 2
  • 6
  • 13
  • Have you tried my code? Are you still facing problems? – Searock Aug 17 '10 at 14:34
  • Does this answer your question? [Show/Hide the console window of a C# console application](https://stackoverflow.com/questions/3571627/show-hide-the-console-window-of-a-c-sharp-console-application) – StayOnTarget Mar 04 '22 at 18:24

6 Answers6

40

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.

Josh
  • 68,005
  • 14
  • 144
  • 156
  • Ah, I see. So what would that be under "New Project" in Visual Studio? "Empty Project"? 'Cause I don't see an option for a normal .exe, just some forms and a console application. – Kratz Aug 16 '10 at 21:51
  • 3
    You can just right click the project, choose properties, then change the output type from Console Application to Windows Application. No other change should be necessary. – Josh Aug 16 '10 at 21:52
  • Okay, I did that but now my application won't work :/ When it was a console application it used to make directories and send a web request... now it won't do any of that. – Kratz Aug 16 '10 at 21:58
  • I can't think of any reason that simply changing the output type would cause any of that. Perhaps you changed something else while trying to hide the console window? – Josh Aug 17 '10 at 03:48
19

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.

Community
  • 1
  • 1
Searock
  • 6,278
  • 11
  • 62
  • 98
  • 1
    This works, but shows a short flash of a console window when you start the process from another process. – Wouter Jul 21 '14 at 14:19
9

Have you tried: Project Properties> Application > output Type: to "Windows Application"?

NinjaCat
  • 9,974
  • 9
  • 44
  • 64
  • I just did that and now the application is broken, it is meant to make directories and copy files from an Embedded Resource and now it doesn't work. :( – Kratz Aug 16 '10 at 21:58
2

Hello I was creating a Console application to be called by the task scheduler. I didn't want the console app to show up so I changed the project properties to have the output to Windows Application.

Change the output type to Windows application Go to : Project - >Project Properties And change the output type to Windows Application

  • to make it hidden through the task scheduler, all you have to do is check "run even if user is not logged in" when creating the task. – Heriberto Lugo Mar 12 '18 at 17:05
2

Its a little more complicated than a console application... but if you want something to truly be running in the background when someone logs in then you could create a Windows Service application.

But it does require a little additional work in setting up and installing the Windows Service but there is an abundance of example code on the web:

http://msdn.microsoft.com/en-us/library/9k985bc9(v=VS.80).aspx

http://msdn.microsoft.com/en-us/library/sd8zc8ha(v=VS.80).aspx

http://www.c-sharpcorner.com/uploadfile/mahesh/window_service11262005045007am/window_service.aspx

http://www.developer.com/net/net/article.php/2173801/Creating-a-Windows-Service-in-NET.htm

http://www.codeproject.com/KB/dotnet/simplewindowsservice.aspx

Lewray
  • 1,164
  • 1
  • 11
  • 26
  • 1
    One caviat is that a service will start before the user logs in, and isn't tied to a specific user account. Depending on what you're doing this can be useful or cause headaches. – Kelly Elton Mar 04 '14 at 22:15
0

I tried both methods 2) Searock and then 1) Josh --- with Searock's solution the console app window still appeared, although for a very brief moment --- however with Josh's solution the console did not appear nor did my program have any issues -- of course I did have to replace all the console.writeline calls with a call that logged the information out to a log file

Note: I would have just commented on Josh's solution but I cannot do that quite yet :)

Dennis Jensen
  • 214
  • 1
  • 14