Skip to content

Instantly share code, notes, and snippets.

@alex-berezan
Created October 25, 2013 20:44
Show Gist options
  • Save alex-berezan/7161537 to your computer and use it in GitHub Desktop.
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.
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