How to write to the stdin of another app?

后端 未结 3 1685
故里飘歌
故里飘歌 2020-12-10 16:48

I have a module that reads the StandardError of a process. Everything works fine, but I want to do something different. I don\'t know how to redirect stdin like the native w

3条回答
  •  佛祖请我去吃肉
    2020-12-10 17:03

    Once you have the Process object representing the process you want to communicate with (either because you got it from the OS using the static method Process.GetProcesses, or because you created it yourself), you can attach a StreamWriter to the Process.StandardInput property and use that to write to the stdin of the process. Don't forget to set ProcessStartInfo.RedirectStandardInput on your newly created process to true.

    On a related note, you can use a StreamReader on the Process.StandardOutput to read the output (like reading from StandardError).

提交回复
热议问题