2009-06-01 277 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

只需将“计划任务”配置为“运行用户是否已登录”。