简体   繁体   中英

C# .NET Process class: How do I send input to a console process that is waiting for input?

Here's the situation: Our C# WPF application needs to parse the output that a 3rd-party console application writes to a file. Starting the process and parsing the file after WaitForExit() would be easy enough, but unfortunately the app was written such that after 20 lines of output, it stops output and waits for the user to hit return. Yes, even though it writes its output to a file, it will still pause and wait for the user to hit return before writing the next 20 lines. The 3rd-party app is completely closed source and there's no hope of recompiling it to disable this behavior. It's quirky in another respect, too: Despite being a console app, it must be run within it's own directory. Given that it's a win32 port of an old DOS app, I guess I shouldn't be surprised.

I'm aware of the usual solutions like WaitForInputIdle() or hacky win32 stuff, but I can't really find anything that suits my situation. Anything that involves finding an open window and sending input via win32 isn't an option for us since the console app can't be run in a window.

So what do you think? Have I not found the right win32 hack yet? Maybe I should wrap the console app in a winform and then I can use WaitForInputIdle()?

If it's a normal console app, simply set UseShellExecute to false and RedirectStandardInput to true , then use the StandardInput property to write a newline when needed.

Using files is an extremely outdated approach, at least 20 years past. Whenever you find yourself using them, you should consider modern standard input/output piping.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM