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.
views:
416answers:
3
+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
2009-11-05 19:52:56
Why the downvotes?
Reed Copsey
2009-11-05 19:59:02
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
2009-11-05 19:54:34
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
2009-11-05 20:15:05
+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
2009-11-05 19:58:02