2009-06-01 250 views
22

我有一個控制檯應用程序用於通過Windows調度程序運行預定作業。應用程序的所有通信都在電子郵件,事件日誌記錄和數據庫日誌中。有沒有辦法阻止控制檯窗口出現?.Net無法啓動控制檯的控制檯應用程序

回答

44

當然。將它構建爲一個WinForms應用程序,絕不顯示您的表單。

只是要小心,因爲它不再是一個真正的控制檯應用程序,並且在某些環境下您將無法使用它。

+0

任何方式,我可以在現有的項目中做到這一點,所以我不必遷移的東西? – Jeff 2009-06-01 13:51:46

+16

右鍵單擊該項目,轉到屬性,然後在彈出的窗體中將其從控制檯應用程序更改爲WinForms應用程序,關閉並重新編譯。 – 2009-06-01 13:53:53

+0

謝謝克里斯,那非常棒! – Jeff 2009-06-01 14:04:14

1

爲什麼不讓應用程序成爲Windows服務?

9

從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 
     } 
2

計劃任務運行作爲與您的帳戶不同的用戶,您將不會彈出一個窗口。 。 。

2

只需將「計劃任務」配置爲「運行用戶是否已登錄」。