Created
October 25, 2013 20:44
-
-
Save alex-berezan/7161537 to your computer and use it in GitHub Desktop.
Executes specified command line passing given arguments. Logs process output and exit code.
This file contains 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
private static void ExecuteCommandLine(string fileName, string[] arguments) | |
{ | |
Process process = new Process | |
{ | |
StartInfo = new ProcessStartInfo | |
{ | |
FileName = fileName, | |
Arguments = string.Join(" ", arguments.Select(x => string.Format("\"{0}\"", x))), | |
RedirectStandardOutput = true, | |
RedirectStandardError = true, | |
CreateNoWindow = true, | |
UseShellExecute = false, | |
} | |
}; | |
string commandLine = string.Format("{0} {1}", fileName, string.Join(" ", arguments)); | |
Logger.Debug("Executing: '{0}' ...", commandLine); | |
var sb = new StringBuilder(); | |
try | |
{ | |
process.Start(); | |
while (!process.StandardOutput.EndOfStream) | |
{ | |
sb.AppendLine(process.StandardOutput.ReadLine()); | |
} | |
} | |
catch (Exception exc) | |
{ | |
Logger.Error(exc.ToString()); | |
} | |
string message = string.Format("Process returned exit code {1}.{0}Output:{0}{2}", Environment.NewLine, process.ExitCode, sb); | |
if (process.ExitCode == 0) | |
{ | |
Logger.Debug(message); | |
} | |
else | |
{ | |
Logger.Error(message); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment