问题
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