如何在隐藏控制台的情况下运行 C# 控制台应用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/836427/
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
How to run a C# console application with the console hidden
提问by Aaron Thomas
Is there a way to hide the console window when executing a console application?
有没有办法在执行控制台应用程序时隐藏控制台窗口?
I am currently using a Windows Forms application to start a console process, but I don't want the console window to be displayed while the task is running.
我目前正在使用 Windows 窗体应用程序来启动控制台进程,但我不希望在任务运行时显示控制台窗口。
采纳答案by Adam Markowitz
If you are using the ProcessStartInfo
class you can set the window style to hidden - in the case of console (not GUI) applications, you have to set CreateNoWindow to true
:
如果您正在使用ProcessStartInfo
该类,您可以将窗口样式设置为隐藏 - 在控制台(不是 GUI)应用程序的情况下,您必须将 CreateNoWindow 设置为true
:
System.Diagnostics.ProcessStartInfo start =
new System.Diagnostics.ProcessStartInfo();
start.FileName = dir + @"\Myprocesstostart.exe";
start.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; //Hides GUI
start.CreateNoWindow = true; //Hides console
回答by Nathan Koop
I know I'm not answering exactly what you want, but I am wondering if you're asking the right question.
我知道我没有完全回答你想要的,但我想知道你是否在问正确的问题。
Why don't you use either:
你为什么不使用:
- windows service
- create a new thread and run your process on that
- 窗口服务
- 创建一个新线程并在其上运行您的进程
Those sound like better options if all you want is to run a process.
如果您只想运行一个进程,这些听起来是更好的选择。
回答by Thomas Levesque
You can use the FreeConsoleAPI to detach the console from the process :
您可以使用FreeConsoleAPI 将控制台与进程分离:
[DllImport("kernel32.dll")]
static extern bool FreeConsole();
(of course this is applicable only if you have access to the console application's source code)
(当然,这仅在您有权访问控制台应用程序的源代码时才适用)
回答by Chris Bregg
If you're creating a program that doesn't require user input you could always just create it as a service. A service won't show any kind of UI.
如果您正在创建一个不需要用户输入的程序,您可以随时将其创建为服务。服务不会显示任何类型的 UI。
回答by Simon
If you wrote the console application you can make it hidden by default.
如果您编写了控制台应用程序,则可以默认将其隐藏。
Create a new console app then then change the "Output Type" type to "Windows Application" (done in the project properties)
创建一个新的控制台应用程序,然后将“输出类型”类型更改为“Windows 应用程序”(在项目属性中完成)
回答by Simon
If you are using Process Class then you can write
如果您使用的是流程类,那么您可以编写
yourprocess.StartInfo.UseShellExecute = false;
yourprocess.StartInfo.CreateNoWindow = true;
before yourprocess.start();
and process will be hidden
beforeyourprocess.start();
和 process 将被隐藏
回答by Cihan
Simple answer is that: Go to your console app's properties(project's properties).In the "Application" tab, just change the "Output type" to "Windows Application". That's all.
简单的答案是:转到您的控制台应用程序的属性(项目的属性)。在“应用程序”选项卡中,只需将“输出类型”更改为“Windows 应用程序”。就这样。
回答by kjbartel
Although as other answers here have said you can change the "Output type" to "Windows Application", please be aware that this will mean that you cannot use Console.In
as it will become a NullStreamReader.
尽管正如这里的其他答案所说,您可以将“输出类型”更改为“Windows 应用程序”,但请注意,这意味着您无法使用Console.In
,因为它将成为 NullStreamReader。
Console.Out
and Console.Error
seem to still work fine however.
Console.Out
并且Console.Error
似乎仍然工作正常不过。
回答by JCH2k
If you're interested in the output, you can use this function:
如果您对输出感兴趣,可以使用此函数:
private static string ExecCommand(string filename, string arguments)
{
Process process = new Process();
ProcessStartInfo psi = new ProcessStartInfo(filename);
psi.Arguments = arguments;
psi.CreateNoWindow = true;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
psi.UseShellExecute = false;
process.StartInfo = psi;
StringBuilder output = new StringBuilder();
process.OutputDataReceived += (sender, e) => { output.AppendLine(e.Data); };
process.ErrorDataReceived += (sender, e) => { output.AppendLine(e.Data); };
// run the process
process.Start();
// start reading output to events
process.BeginOutputReadLine();
process.BeginErrorReadLine();
// wait for process to exit
process.WaitForExit();
if (process.ExitCode != 0)
throw new Exception("Command " + psi.FileName + " returned exit code " + process.ExitCode);
return output.ToString();
}
It runs the given command line program, waits for it to finish and returns the output as string.
它运行给定的命令行程序,等待它完成并以字符串形式返回输出。
回答by IDK anything
Add this to your class to import the DLL file:
将此添加到您的类以导入 DLL 文件:
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
[DllImport("kernel32.dll")]
static extern IntPtr GetConsoleWindow();
const int SW_HIDE = 0;
const int SW_SHOW = 5;
And then if you want to hide it use this command:
然后,如果您想隐藏它,请使用以下命令:
var handle = GetConsoleWindow();
ShowWindow(handle, SW_HIDE);
And if you want to show the console:
如果你想显示控制台:
var handle = GetConsoleWindow();
ShowWindow(handle, SW_SHOW);