Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.Net Console Application that Doesn't Bring up a Console

Tags:

I have a console application I'm using to run scheduled jobs through windows scheduler. All the communication to/from the application is in email, event logging, database logs. Is there any way I can suppress the console window from coming up?

like image 749
Jeff Avatar asked Jun 01 '09 13:06

Jeff


People also ask

How do I run a console application without showing the console?

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.

How do I stop a console program in C#?

You can use Environment. Exit(0) and Application. Exit .

What is .NET console application?

Console applications are designed to be used from a "text-only" interface, or run without any interface by another automated tool.


2 Answers

Sure. Build it as a winforms app and never show your form.

Just be careful, because then it's not really a console app anymore, and there are some environments where you won't be able to use it.

like image 140
Joel Coehoorn Avatar answered Sep 18 '22 16:09

Joel Coehoorn


Borrowed from MSDN (link text):

using System.Runtime.InteropServices;  ...       [DllImport("user32.dll")]       public static extern IntPtr FindWindow(string lpClassName,string lpWindowName);        [DllImport("user32.dll")]       static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);  ...           //Sometimes System.Windows.Forms.Application.ExecutablePath works for the caption depending on the system you are running under.          IntPtr hWnd = FindWindow(null, "Your console windows caption"); //put your console window caption here          if(hWnd != IntPtr.Zero)          {             //Hide the window             ShowWindow(hWnd, 0); // 0 = SW_HIDE          }            if(hWnd != IntPtr.Zero)          {             //Show window again             ShowWindow(hWnd, 1); //1 = SW_SHOWNORMA          } 
like image 39
MPritchard Avatar answered Sep 19 '22 16:09

MPritchard