Legend
Legend

Reputation: 116800

Executing a unix style piping command?

Is there a way to execute the following command through C#?

.\binary.exe < input > output

I am trying to use System.Diagnostics.Process but I was wondering if there is a direct exec style command in C#. Any suggestions?

Upvotes: 0

Views: 685

Answers (2)

chemicalNova
chemicalNova

Reputation: 841

Not directly, but you can redirect output from a console stream (as you may have figured out, considering you're trying to use the Process class), as noted here on MSDN: http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput.aspx

There is also an example here on SO: Redirect console output to textbox in separate program

Wrap that into your own class and it will basically become an "exec" style command.

Upvotes: 1

Daniel
Daniel

Reputation: 31569

Basically you need to redirect the standard input and output to your program and write them to the files you want

ProcessStartInfo info = new ProcessStartInfo("binary.exe");
info.RedirectStandardInput = true;
info.RedirectStandardOutput = true;
Process p = Process.Start(info);

string Input;
// Read input file into Input here

StreamWriter w = new StreamWriter(p.StandardInput);
w.Write(Input);
w.Dispose();

StreamReader r = new StreamReader(p.StandardOutput);
string Output = r.ReadToEnd();
r.Dispose();

// Write Output to the output file

p.WaitForExit();

Upvotes: 1

Related Questions