Skip to content

Instantly share code, notes, and snippets.

@corngood
Created January 4, 2017 15:53
Show Gist options
  • Save corngood/d982c3c21c016127a2f1600dc895c000 to your computer and use it in GitHub Desktop.
Save corngood/d982c3c21c016127a2f1600dc895c000 to your computer and use it in GitHub Desktop.
C# Process wrapper
using System;
using System.Diagnostics;
using System.Linq;
static class Program {
static int Main(string[] args) {
var p = new ProcessStartInfo {
FileName = args[0],
// TODO: quote args
Arguments = String.Join(" ", args.Skip(1).ToArray()),
RedirectStandardInput = true,
RedirectStandardError = true,
RedirectStandardOutput = true,
UseShellExecute = false
};
var process = Process.Start(p);
process.BeginOutputReadLine();
process.OutputDataReceived += (s, e) => Console.Out.WriteLine(e.Data);
process.BeginErrorReadLine();
process.OutputDataReceived += (s, e) => Console.Error.WriteLine(e.Data);
string l;
while ((l = Console.ReadLine()) != null) {
process.StandardInput.WriteLine(l);
process.StandardInput.Flush();
}
process.StandardInput.Close();
process.WaitForExit();
return process.ExitCode;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment