1

I am trying to redirect the output of my command line program when it is called with Process.Start(). I've tried all permutations of RedirectStandardOutput, UseShellExecute and CreateNowWindow and had no luck.

I know there are other ways of accomplishing this such as reading StandardOutput after this but I would prefer to redirect it with arguments if possible into a file. I only care about the return code and only need the output of myprog.exe to go to a file. My program doesn't need to know what the output is.

var p = new Process();
p.StartInfo.FileName = @"myprog.exe";
p.StartInfo.Arguments = " " + InputFilename + " > " + OutputFilename;

p.StartInfo.RedirectStandardOutput = false;
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;

p.Start();
p.WaitForExit();
var result = p.ExitCode;
  • 1
    Does this answer your question? [ProcessInfo and RedirectStandardOutput](https://stackoverflow.com/questions/1145969/processinfo-and-redirectstandardoutput) – Suresh May 28 '20 at 14:03
  • Not really. I only care about the output from myprog.exe going to a file. My program doesn't need to do anything with the output of myprog.exe except for get the return value. – user1620479 May 28 '20 at 14:14
  • Did you try `cmd.exe` for `p.StartInfo.FileName` and `myprog.exe inputfileName > outputFileName` for `p.StartInfo.Arguments`? – Suresh May 28 '20 at 14:19
  • I didn't think of that but I tried it and it didn't work. The program locked up. – user1620479 May 28 '20 at 14:26
  • If you are using redirection (file redirection like > filename) you need ShellExecute = true, as that is a shell (cmd) function. Alternatively you can change your command to invoke a shell directly be like "cmd.exe /c dir > file" – Milney May 28 '20 at 14:44

1 Answers1

-1

This will work for my issue. Get the output with p.StandardOutput.ReadToEnd() and write it to a file.