0

I know there are several post about passing arguments to a program but I haven't been able to solve my problem.

I am trying to pass some arguments to a program from my c# code.

When adding the arguments /c < input.txt | program2 > output.txt I get an System.InvalidOperationException.

With no arguments passed, the code works fine and program1 starts.

I have not been able to figure out how to explain my arguments correct.

Possibly the use of <, >, | and the fact that there is a second program among the arguments gets me into trouble?

This works fine in cmd.

My code:

var myProcess = new System.Diagnostics.Process
{
    StartInfo = new System.Diagnostics.ProcessStartInfo
    {
        FileName = "program1",
        Arguments = "/c < input.txt | program2 > output.txt",
        UseShellExecute = false,
        RedirectStandardOutput = true,
        CreateNoWindow = false
    }
};

myProcess.Start();
while (!myProcess.StandardOutput.EndOfStream)
{
    string line = myProcess.StandardOutput.ReadLine();
}
myProcess.WaitForExit();
absoluteAquarian
  • 510
  • 3
  • 12
lem2015
  • 1
  • 1
  • 1
    You may need a different approach for handling input/output redirection or simply break up the calls to the two programs into different steps. See https://stackoverflow.com/a/21848564/3608792. – Dan Wilson Oct 05 '18 at 14:40
  • 4
    For piping I/O you need `UseShellExecute=true` - it's the shell that does the actual piping. – 500 - Internal Server Error Oct 05 '18 at 14:48

0 Answers0