0

Possible Duplicate:
Redirect Standard Output Efficiently in .NET
Capturing console output from a .NET application (C#)

I know how to execute something like this:

SomeEXE inputfile.txt

in the command prompt via C#.

The problem I am having is that SomeEXE opens another command prompt where it writes the outputs given inputfile.txt.

Is it generally possible to obtain these outputs? Thanks.

Here is my current code:

System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();

startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C SomeEXE inputfile.txt";
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;

process.StartInfo = startInfo;
process.Start();

// Now use streams to capture the output
StreamReader outputReader = process.StandardOutput;

process.WaitForExit();

String line = outputReader.ReadToEnd();
Community
  • 1
  • 1
cs0815
  • 16,751
  • 45
  • 136
  • 299
  • Take a look @ http://stackoverflow.com/questions/186822/capturing-console-output-from-a-net-application-c – Alex K. Sep 11 '12 at 14:33
  • http://stackoverflow.com/search?q=console+redirect – Sam Axe Sep 11 '12 at 14:34
  • @Dan-o more like http://stackoverflow.com/search?q=console+redirect+%5Bc%23%5D&submit=search – Chuck Dee Sep 11 '12 at 14:35
  • @wraith808: that will work too :) – Sam Axe Sep 11 '12 at 14:36
  • @Steve B I am not doing anything wrong. I just wonder whether it is possible. – cs0815 Sep 11 '12 at 14:37
  • @Alex K. I am aware of this and the comment does not help at all. – cs0815 Sep 11 '12 at 14:37
  • Then stating why that standard redirection approach fails in your scenario would be helpful – Alex K. Sep 11 '12 at 14:41
  • The question states that it opens another command prompt. – cs0815 Sep 11 '12 at 14:59
  • I really do not understand why you close this. I am aware of the other stuff. The problem is that the command, after being issued from C#, opens another command prompt. This command prompt contains all the output! I believe I stated this clearly: SomeEXE opens another command prompt – cs0815 Sep 11 '12 at 15:16
  • I have rewritten my original question here: http://stackoverflow.com/questions/12373515/command-issued-via-c-sharp-opens-another-command-prompt – cs0815 Sep 11 '12 at 15:51

1 Answers1

1
ProcessStartInfo processStartInfo = new processStartInfo("SomeEXE", "inputfile.txt");
processStartInfo.UseShellExecute = false;
processStartInfo.ErrorDialog = false;

// Here is where you grab the output:
processStartInfo.RedirectStandardOutput = true;

Process process = new Process {
  StartInfo = processStartInfo
};
process.Start();

// Now use streams to capture the output
StreamReader outputReader = process.StandardOutput;

process.WaitForExit();

Now you can read the outputStream as necessary.

I am guessing this is what you mean. Also, here are the docs on RedirectStandardOutput

Also, if you know the path to the file that was generated (assuming the SomeEXE wrote to another file) you can use File.Open to access its contents after SomeEXE has executed (remember to wait until after otherwise SomeEXE may still have a handle on the file making it difficult to read it).

Brad Christie
  • 100,477
  • 16
  • 156
  • 200
  • This does not work as the command opens another command prompt where it also writes the output (no output file). Furthermore the other command prompt wait for input (carriage return). – cs0815 Sep 11 '12 at 15:13
  • So the first app (launched by `Process`) launches another window executing another application which is the output you _really_ want? – Brad Christie Sep 11 '12 at 15:22
  • Exactly. I thought I made that clear in my original question - sorry if not (-: – cs0815 Sep 11 '12 at 15:35