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

青春壹個敷衍的年華 提交于 2019-11-27 06:35:13

问题


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?


回答1:


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.




回答2:


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
         }



回答3:


It's a hack, but the following blog post describes how you can hide the console window:

http://expsharing.blogspot.com/2008/03/hideshow-console-window-in-net-black.html




回答4:


Schedule the task to run as a different user than your account and you won't get a window popping up . . .




回答5:


Simply configure the Scheduled Task as "Run whether user is logged on or not".




回答6:


Why don't you make the application a Windows Service?



来源:https://stackoverflow.com/questions/934901/net-console-application-that-doesnt-bring-up-a-console

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!