在同一控制台中启动进程
我可以在与调用程序相同的控制台中启动进程(使用 C# Process.Start()
)吗?这样就不会创建新窗口,并且标准输入/输出/错误将与调用控制台应用程序相同。我尝试设置 process.StartInfo.CreateNoWindow = true; 但该进程仍然在新窗口中启动(并在完成后立即关闭)。
Can I start a process (using C# Process.Start()
) in the same console as the calling program? This way no new window will be created and standard input/output/error will be the same as the calling console application. I tried setting process.StartInfo.CreateNoWindow = true;
but the process still starts in a new window (and immediately closes after it finishes).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
除了设置
UseShellExecute = false
之外,您不需要执行任何操作,因为 Win32 CreateProcess 函数用于控制台应用程序继承其父控制台,除非您指定 CREATE_NEW_CONSOLE 标志。我尝试了以下程序:
它给了我这个输出:
You shouldn't need to do anything other than set
UseShellExecute = false
, as the default behaviour for the Win32 CreateProcess function is for a console application to inherit its parent's console, unless you specify the CREATE_NEW_CONSOLE flag.I tried the following program:
and it gave me this output:
您可以尝试重定向此进程的输出,然后将其打印在调用进程控制台上:
使用
Exited
事件和等待句柄的替代方法:You could try redirecting the output of this process and then printing it on the calling process console:
Alternative approach using the
Exited
event and a wait handle: