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

自闭症网瘾萝莉.ら 提交于 2019-11-28 11:54:28

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.

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
         }

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

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

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

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

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