在不干扰控制台窗口的情况下在 C# 中启动进程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/739101/
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
Launching process in C# Without Distracting Console Window
提问by
I figure out how to launch a process. But my problem now is the console window (in this case 7z) pops up frontmost blocking my vision and removing my focus interrupting my sentence or w/e i am doing every few seconds. Its extremely annoying, how do i prevent that from happening. I thought CreateNoWindow solves that but it didnt.
我想出了如何启动一个进程。但我现在的问题是控制台窗口(在本例中为 7z)弹出最前面,挡住了我的视线并移开了我的注意力,打断了我的句子或 w/ei 每隔几秒钟。它非常烦人,我如何防止这种情况发生。我认为 CreateNoWindow 解决了这个问题,但没有。
NOTE: sometimes the console needs user input (replace file or not). So hiding it completely may be a problems a well.
注意:有时控制台需要用户输入(是否替换文件)。所以完全隐藏它可能是一个问题。
This is my current code.
这是我目前的代码。
void doSomething(...)
{
myProcess.StartInfo.FileName = ...;
myProcess.StartInfo.Arguments = ...;
myProcess.StartInfo.CreateNoWindow = true;
myProcess.Start();
myProcess.WaitForExit();
}
回答by galets
Try this:
尝试这个:
myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;
回答by Paul Alexander
I'll have to double check, but I believe you also need to set UseShellExecute = false
. This also lets you capture the standard output/error streams.
我必须仔细检查,但我相信您还需要设置UseShellExecute = false
. 这还可以让您捕获标准输出/错误流。
回答by Mun
If I recall correctly, this worked for me
如果我没记错的话,这对我有用
Process process = new Process();
// Stop the process from opening a new window
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
// Setup executable and parameters
process.StartInfo.FileName = @"c:\test.exe"
process.StartInfo.Arguments = "--test";
// Go
process.Start();
I've been using this from within a C# console application to launch another process, and it stops the application from launching it in a separate window, instead keeping everything in the same window.
我一直在 C# 控制台应用程序中使用它来启动另一个进程,它阻止应用程序在单独的窗口中启动它,而是将所有内容都保留在同一个窗口中。
回答by codefox
@galets In your suggestion, the window is still created, only it begins minimized. This would work better for actually doing what acidzombie24 wanted:
@galets 在你的建议中,窗口仍然被创建,只是它开始最小化。这对于实际执行 acidzombie24 想要的操作会更好:
myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;