Skip to content

Instantly share code, notes, and snippets.

@puncha
Created May 22, 2014 09:51
Show Gist options
  • Save puncha/27b6b1e61f294294838c to your computer and use it in GitHub Desktop.
Save puncha/27b6b1e61f294294838c to your computer and use it in GitHub Desktop.
Run Shell Command and watch its output
try
{
Process process = new Process();
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.FileName = "cmd";
process.StartInfo.Arguments = "/C " + command;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.WorkingDirectory = "Something";
process.OutputDataReceived += (sender, e) =>
{
if (e.Data == null)
return;
Console.WriteLine(e.Data);
sb.AppendLine(e.Data);
NotifyToInputPassword(e.Data);
};
process.ErrorDataReceived += (sender, e) => Console.Write(e.Data);
process.Start();
process.BeginOutputReadLine();
process.WaitForExit();
return process.ExitCode;
}
catch (Exception exp)
{
return -1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment