.Net控制台应用程序无法启动控制台

Jef*_*eff 22 .net c# vb.net console console-application

我有一个控制台应用程序,我用来通过Windows调度程序运行预定的作业.与应用程序之间的所有通信都在电子邮件,事件记录,数据库日志中.有什么方法可以抑制控制台窗口的出现吗?

Joe*_*orn 45

当然.将其构建为winforms应用程序,永远不会显示您的表单.

请注意,因为它不再是一个真正的控制台应用程序,并且在某些环境中您将无法使用它.

  • 右键单击项目,转到"属性",然后在弹出的窗体中,将其从Console应用程序更改为WinForms应用程序,关闭并重新编译. (16认同)

MPr*_*ard 10

借鉴MSDN(链接文字):

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
         }
Run Code Online (Sandbox Code Playgroud)