Run interactive process inside already-running console application without opening new window

泄露秘密 提交于 2019-11-28 14:16:37

问题


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:

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.

How do you do this? Set ProcessStartInfo.CreateNoWindow, or ProcessStartInfo.WindowStyle? Try to make input/output redirection work for hours on end?


EDIT: smh... this is different from the "possible duplicates" out there because:

  • 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.

回答1:


Simple: ProcessStartInfo.UseShellExecute=false. That's it! (I know, I couldn't believe it myself).

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!



来源:https://stackoverflow.com/questions/47781911/run-interactive-process-inside-already-running-console-application-without-openi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!