tags:

views:

70

answers:

4

So say I run my program:

Process proc = new Process();
proc.StartInfo.FileName = Path.GetDirectoryName(Application.ExecutablePath)
                           + @"\Console.exe";
proc.Start();

And then wish to output my console stream to this application, how would I go about doing so? So say I have:

Console.WriteLine("HEY!");

I want that to show up in program that I ran's console. I know I have to redirect the output using

Console.SetOut(TextWriter);

But I have no idea how I would go about making it write to the other program.

I can see how I could do it if I were running my main program from Console.exe using RedirectStandardInput.. but that doesn't really help :P

A: 

You need to use Process.StandardOutput and Process.StandardInput. Check out this article from MSDN, which may help point you into the right direction: http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput.aspx

By the way, a much easier way to do what you are doing can be found here, as an accepted answer to a similar SO question: http://stackoverflow.com/questions/1290747/c-redirect-pipe-process-output-to-another-process

icemanind
+2  A: 

RedirectStandardInput makes Console.exe take its input from a stream which you can access in the main program. You can either write directly to that stream, or use SetOut to redirect console output there...

Process proc = new Process();
proc.StartInfo.FileName = Path.GetDirectoryName(Application.ExecutablePath)
                       + @"\Console.exe";
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardInput = true;
proc.Start();

proc.StandardInput.WriteLine("Hello");

Console.SetOut(proc.StandardInput);
Console.WriteLine("World");

EDIT

It's possible that Console.exe doesn't cope well with having data piped into it rather than entered interactively. You could check this from the command line with something like

echo "Hello" | Console.exe

If that doesn't do what you expect, redirecting your program's output won't either. To test your program without worrying about the target program, you could try

proc.StartInfo.FileName = @"cmd";
proc.StartInfo.Arguments = @"/C ""more""";

If that displays the text that you write to it, then the problem is on the receiving end.

stevemegson
I have pretty much this exact code, yet it doesn't seem to want to actually output to the other program :\proc.StandardInput.WriteLine("t1");for example doesn't seem to write, I've tried flushing it but nothing happens :(
Blam
@Blam: Edited with some other things to try
stevemegson
Eventually got it working, just had to change my Console.exe program, thanks :)
Blam
A: 

There's a lot of similar questions in StackOverflow. Consider using the search box: http://stackoverflow.com/search?q=c%23+redirect+output+SetOut

I find this thread quite inspiring: http://stackoverflow.com/questions/415620/redirect-console-output-to-textbox-in-separate-program-c

Once you know the fd used by the process: http://cboard.cprogramming.com/c-programming/125352-how-write-fd-instead-terminal-execv.html

karlphillip
All the ones I saw did the opposite, aka: reading in from launched applications instead of writing out to them. :)
Blam
+1  A: 

RedirectStandardInput isn't the problem. Console is the problem.

 StreamWriter myConsole = null;
if (redirect)
{
 Process proc = new Process(); 
 proc.StartInfo.FileName = Path.GetDirectoryName(Application.ExecutablePath) 
                       + @"\Console.exe"; 
 proc.StartInfo.UseShellExecute = false;
 proc.StartInfo.RedirectStandardInput = true;
 proc.Start(); 
 myConsole = myProcess.StandardInput;
 }
 else
    myConsole = Console.Out;

Then just use myConsole as you would Console.

James Curran