开发者

How do I redirect the input and output of a console C# program to a socket?

How do I redirect the input and output of a console C# program to a socket? I need to start the program from within another process, and make it 开发者_如何学运维receive input and emit output to another process running on another computer in the network. What I'm trying to do is to programmatically run an application on another station, while controlling it from the current terminal.


Use the System.Diagnostics.Process class to run your console program.

System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;

And then you can hook any stream (including networkstreams) up to process.StandardInput and process.StandardOutput.


You'll need to setup ProcessStartInfo.RedirectStandardInput and RedirectStandardOutput. This lets you provide a stream to use for standard input/output of the console application.

You can then copy data from the process streams to and from the socket's stream.


Why redirect?

If this is core functionality of your application, then you should have Stream variables that your code uses rather than relying on Console.Out, etc.

The stream can be opened from a socket, a file, whatever right in code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜