Created
December 20, 2017 17:30
-
-
Save MerlinTwi/cadcfcb34bda4ca1ad97b1560c771850 to your computer and use it in GitHub Desktop.
Execute third-party programs from Unity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.IO; | |
| public static class ProcessExecute { | |
| public static int Execute(string command, string arguments, out string output, out string error, string input = null) { | |
| var proc = new System.Diagnostics.Process(); | |
| proc.StartInfo.UseShellExecute = false; | |
| if (!string.IsNullOrEmpty(input)) | |
| proc.StartInfo.RedirectStandardInput = true; | |
| proc.StartInfo.RedirectStandardError = true; | |
| proc.StartInfo.RedirectStandardOutput = true; | |
| proc.StartInfo.FileName = command; | |
| proc.StartInfo.Arguments = arguments; | |
| proc.Start(); | |
| if (!string.IsNullOrEmpty(input)) { | |
| // Unity's old Mono runtime writes a BOM to the input stream, | |
| // tripping up the command. Ceate a new writer with an encoding | |
| // that has BOM disabled. | |
| var writer = new StreamWriter(proc.StandardInput.BaseStream, new System.Text.UTF8Encoding(false)); | |
| writer.Write(input); | |
| writer.Close(); | |
| } | |
| proc.WaitForExit(); | |
| output = proc.StandardOutput.ReadToEnd(); | |
| error = proc.StandardError.ReadToEnd(); | |
| return proc.ExitCode; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment