在已经运行的控制台应用程序中运行交互过程,而无需打开新窗口 [英] Run interactive process inside already-running console application without opening new window
问题描述
我意识到这看起来还有很多其他问题,但是我看了几个小时都没找到真正需要的答案,所以请听我说:
I realize this looks like a lot of other questions out there, but I looked at all of them for hours and never found the real answer I needed, so hear me out:
这是用于.NET C#控制台应用程序。在其中,我想使用 Process.Start
调用Windows可执行文件,但在运行时没有打开新的控制台窗口。我还希望可执行文件能够输出到控制台并正常响应用户输入。
This is for a .NET C# console application. Within it, I wanted to call a Windows executable using Process.Start
, but without it opening a new console window when run. I also wanted the executable to be able to output to the console and respond to user input normally.
您如何执行此操作?设置 ProcessStartInfo.CreateNoWindow
还是 ProcessStartInfo.WindowStyle
?尝试使输入/输出重定向连续工作数小时吗?
How do you do this? Set ProcessStartInfo.CreateNoWindow
, or ProcessStartInfo.WindowStyle
? Try to make input/output redirection work for hours on end?
编辑: smh 。 ..这与那里的可能重复项不同,因为:
smh... this is different from the "possible duplicates" out there because:
- 这是用于要运行的控制台应用程序Windows命令在其内部无缝集成,就像该命令是控制台应用程序本身的一部分一样。例如:创建一个新的C#控制台应用程序,并使其运行Windows
copy
命令。 - 此外,我还需要用户能够与正在运行的命令进行交互(例如,如果
copy
询问是否覆盖我希望用户回答的问题),则将窗口隐藏为此,或此可能的答案是否定的。 - 我想强调一下单
UseShellExecute
属性解决了该问题,而没有像 this 或这,这又一次让我浪费了时间。
- This is for a console application that wants to run a Windows command seamlessly within itself, as if the command was part of the console application itself. For example: create a new C# console application and make it run the Windows
copy
command. - Additionally, I needed the user to be able to interact with the running command (e.g., if
copy
asks whether to overwrite I wanted the user to answer), so hiding the window as in this, or this possible answers is a no-no. - I wanted to stress how the single
UseShellExecute
property solves the problem, without the misleading fluff (e.g., about I/O redirection) in answers like this, or this, which again, made me waste hours.
推荐答案
简单: ProcessStartInfo.UseShellExecute = false
。而已! (我知道,我自己也不敢相信。)
Simple: ProcessStartInfo.UseShellExecute=false
. That's it! (I know, I couldn't believe it myself).
经过数小时的各种尝试,这对我有用-甚至不设置 CreateNoWindow = true
!此外,在所有内容的最后,我确实找到了此MSDN帖子证实了我的发现。走吧!
After hours of trying all sorts of things, this is what worked for me - without even setting CreateNoWindow=true
! Also, at the end of it all, I did find this MSDN post that confirms what I found out. Go figure!
这篇关于在已经运行的控制台应用程序中运行交互过程,而无需打开新窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!