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

前端 未结 6 909
闹比i
闹比i 2020-12-11 00:10

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 l

6条回答
  •  不思量自难忘°
    2020-12-11 01:09

    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
             }
    

提交回复
热议问题