views:

416

answers:

3

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.

+1  A: 

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.

Reed Copsey
Why the downvotes?
Reed Copsey
A: 

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.

Frank Krueger
The applications I'll be executing are not written by me and they must also work when launched from their home-station in the console.
luvieere
+1  A: 

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.

Wim Hollebrandse
Thank you, I'll get started on this!
luvieere