C# 不启动控制台的 .Net 控制台应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/934901/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 03:40:15  来源:igfitidea点击:

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

c#.netvb.netconsoleconsole-application

提问by Jeff

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 logs. Is there any way I can suppress the console window from coming up?

我有一个控制台应用程序,用于通过 Windows 调度程序运行计划作业。与应用程序之间的所有通信都在电子邮件、事件日志、数据库日志中。有什么办法可以抑制控制台窗口的出现?

采纳答案by Joel Coehoorn

Sure. Build it as a winforms app and never show your form.

当然。将其构建为一个 winforms 应用程序,并且从不显示您的表单。

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.

请小心,因为那样它就不再是真正的控制台应用程序了,并且在某些环境中您将无法使用它。

回答by Badaro

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

为什么不让应用程序成为 Windows 服务?

回答by Philippe Leybaert

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

http://expsharing.blogspot.com/2008/03/hideshow-console-window-in-net-black.html

回答by MPritchard

Borrowed from MSDN (link text):

借用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
         }

回答by Wyatt Barnett

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

安排任务以与您的帐户不同的用户身份运行,您将不会弹出窗口。. .

回答by Peter Meinl

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

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