Created
May 22, 2014 09:51
-
-
Save puncha/27b6b1e61f294294838c to your computer and use it in GitHub Desktop.
Run Shell Command and watch its output
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
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