0

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?

Legend
  • 113,822
  • 119
  • 272
  • 400
  • C# doesn't have commands. What do you mean? – John Saunders Oct 27 '11 at 02:09
  • @JohnSaunders: I believe the equivalent of command in C# is static member function. – Daniel Oct 27 '11 at 02:13
  • I have no idea what you mean by "command" in this context. I don't know of any programming languages that have "commands", but then, I've been out of school for a while. – John Saunders Oct 27 '11 at 02:16
  • @JohnSaunders: what about bash? you may call it a shell, but its a fully blown language and it has "commands". – Daniel Oct 27 '11 at 03:05
  • @JohnSaunders: I am not sure why this led to a confusion but in Python, I always do this: http://stackoverflow.com/questions/89228/how-to-call-external-command-in-python Of course, they are really not called commands but I was interested in knowing how to execute a command through a C# program. Anyways apologies for any confusion. – Legend Oct 27 '11 at 03:40
  • As you can all tell, I was being pedantic. The lesson was, "use terminology we can all agree on". C# has nothing called commands, and no actual programming language that _I_ know of has commands, so use a term we all know, or show which programming language has "commands" so that we can compare and contrast. And bash is a command shell, not a programming language, and yes,I've created serious bash scripts. – John Saunders Oct 27 '11 at 09:42

2 Answers2

1

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();
Daniel
  • 30,896
  • 18
  • 85
  • 139
1

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.

Community
  • 1
  • 1
chemicalNova
  • 831
  • 5
  • 12