120

The thing is, I really don't want the console window to show up, but the solution should be running. My point here is, I want to keep the application running in the background, without any window coming up.

How can I do that?

Pang
  • 9,564
  • 146
  • 81
  • 122
SOF User
  • 7,590
  • 22
  • 75
  • 121
  • 2
    Can you give us a little more information regarding what the application should do? It sounds like it should be a service or windows application? With a little knowledge about it's purpose, we'd be able to help a lot more and suggest the *best* way to solve this. – Dave Oct 04 '10 at 08:27
  • 1
    Its keylogger application. I used windows service but it can not get key states in windows service – SOF User Oct 04 '10 at 08:32
  • 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:23

7 Answers7

215

Change the output type from Console Application to Windows Application. This can be done under Project -> Properties -> Application in Visual Studio:

alt text

Dirk Vollmar
  • 172,527
  • 53
  • 255
  • 316
  • 5
    Console window flashes and goes back. How do i keep the application running with no window at all? – SOF User Oct 04 '10 at 08:29
  • @SOF User: How do you start the application? By double-clicking in Explorer or from the Start Menu? – Dirk Vollmar Oct 04 '10 at 08:30
  • i have also put Console.ReadLine(); – SOF User Oct 04 '10 at 08:33
  • 19
    Remove the `Console.ReadLine`. It doesn't make any sense if you don't have a console window. – Dirk Vollmar Oct 04 '10 at 08:37
  • @DirkVollmar thank you for your answer, it helped me:) One question - how to close such an app? – karollo Dec 17 '18 at 12:39
  • 1
    @KarolŻurowski: The idea here is that you would use this for apps that either also come with some form of UI (e.g. an icon in the sytem tray) or apps that do a certain task and then exit automatically when done. If you have neither, the app will run in the background until logoff/shutdown or until it is explicitly killed, e.g. using Task Manager. – Dirk Vollmar Dec 17 '18 at 13:32
  • @DirkVollmar Thank you. My application is a self hosted WCF service. When the user launches another program it starts and now I have to figure out how to terminate it when I this user will close the first program. Nevertheless thanks for the reply – karollo Dec 17 '18 at 14:12
  • @KarolŻurowski: Sounds like you are after [hosting your service using WAS](https://learn.microsoft.com/en-us/dotnet/framework/wcf/feature-details/hosting-in-windows-process-activation-service). Did you consider that option? – Dirk Vollmar Dec 17 '18 at 14:31
  • @DirkVollmar Actually I did, but I thought that it runs only on windows server whereas I need my service to run on win 8/10 as well. But it turns out that is can be deployed on win 8/10 so I will definitely give it a closer look, thanks :) – karollo Dec 17 '18 at 17:22
27

Change your application type to a windows application. Your code will still run, but it will have no console window, nor standard windows window unless you create one.

Sekhat
  • 4,435
  • 6
  • 43
  • 50
  • 3
    Console window flashes and goes back. How do i keep the application running with no window at all? – SOF User Oct 04 '10 at 08:30
  • You make sure it doesn't terminate. There really is not much else to say without more information. What does the program do? Does it run something in a loop? – Lasse V. Karlsen Oct 04 '10 at 08:56
14

Instead of Console.Readline/key you can use new ManualResetEvent(false).WaitOne() at last. This works well for me.

Ondrej Janacek
  • 12,486
  • 14
  • 59
  • 93
khaja kamal
  • 141
  • 1
  • 3
5

You can use user32.dll to achieve this without vscode

    class start
    {
        [DllImport("user32.dll")]
        static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
        static void Main()
        {
            IntPtr h = Process.GetCurrentProcess().MainWindowHandle;
            ShowWindow(h, 0);
            // Do the rest

This still flashes the screen but works good enough

the usings are

using System.Runtime.InteropServices;
using System.Diagnostics;

Note im still new to csharp and not perfect so feel free to comment and corrections!

Jonathan Coletti
  • 448
  • 4
  • 13
  • 3
    While the other solution is more popular, I think this solution is underrated! Particularly because I can conditionally show/hide the console window depending on the logic, e.g. by adding compiler directives like "#if DEBUG" – Siavash Mortazavi Jan 23 '22 at 17:09
  • btw, "Process.GetCurrentProcess().MainWindowHandle;" didn't work for me and a solution from https://stackoverflow.com/a/34440934/1854557 introduces another Windows API call that does the job. (.NET 6 on Windows 10) – Siavash Mortazavi Jan 23 '22 at 17:51
3

Maybe you want to try creating a Windows Service application. It will be running in the background, without any UI.

bla
  • 5,350
  • 3
  • 25
  • 26
  • 3
    Windows Service can not get Key pressed events – SOF User Oct 04 '10 at 09:04
  • 1
    Services are severely limited depending on your application, as of Windows Vista they are forced in to Session 0, and thus can not perform anything in other sessions such as show popup messages. – Matt Dec 28 '18 at 22:24
  • 1
    @Matt "cannot perform anything in other sessions" is not entirely true. Here's a code project for a Service that opens an admin command prompt on the logged-in user's desktop. https://www.codeproject.com/Articles/35773/Subverting-Vista-UAC-in-Both-and-bit-Archite – Qodex Apr 09 '19 at 20:14
3

Change the output type from Console Application to Windows Application,

And Instead of Console.Readline/key you can use new ManualResetEvent(false).WaitOne() at the end to keep the app running.

N T
  • 438
  • 6
  • 11
0

To compile your application without a console window

csc.exe /target:winexe *.cs
Hecker011
  • 1
  • 1