0

edit - this is a duplicate from here

I would like to create a gui wrapper (in C#) to an existing CLI application (I only have the binaries of this CLI application). in order to do that I need to run the CLI application, and then parse its output, give more commands (witch depends on the output) and so on.

I know that I can use System.Diagnostics.Process.Start(...) to run a single command. but is there any way to communicate with that thread? or is there any other way to do this in C#?

Community
  • 1
  • 1
  • 2
    Redirect stdin/out, E.g. http://stackoverflow.com/questions/21270842/reading-writing-to-a-command-line-program-in-c-sharp – Alex K. Mar 12 '14 at 15:55
  • Sounds like you need to attach to the IO streams of the process.. dunno if that helps you in your Googling.. look for something like `stdin stdout system.diagnostics.process.start c#` – Michael Coxon Mar 12 '14 at 15:57
  • this will (hopefully) will give me access to the output. but how can I send new input to the processes afterwards? write to the stream? – Amir David Nissan Cohen Mar 12 '14 at 16:10
  • Yes, write to the input stream – Alex K. Mar 12 '14 at 16:16

1 Answers1

1

I suggest using CliWrap for these purposes. It supports running processes, argument formatting, cancellation, piping, and a lot of other things. Much smoother than working with System.Diagnostics.Process directly.

Some examples:

using CliWrap;
using CliWrap.Buffered;

var result = await Cli.Wrap("path/to/exe")
    .WithArguments("--foo bar")
    .ExecuteBufferedAsync();

// Result contains:
// -- result.StandardOutput  (string)
// -- result.StandardError   (string)
// -- result.ExitCode        (int)
// -- result.StartTime       (DateTimeOffset)
// -- result.ExitTime        (DateTimeOffset)
// -- result.RunTime         (TimeSpan)
var cmd = "Hello world" | Cli.Wrap("foo")
    .WithArguments("print random") | Cli.Wrap("bar")
    .WithArguments("reverse") | (Console.WriteLine, Console.Error.WriteLine);

await cmd.ExecuteAsync();
Tyrrrz
  • 2,195
  • 1
  • 19
  • 22