Sus formas son buenas. Pero solo obtienes toda la Salida al final. Quería la salida cuando el script se estaba ejecutando. Así que aquí está, primero más o menos lo mismo, pero de lo que hice la salida. Si tiene problemas miran: http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.aspx
public void execute(string workingDirectory, string command)
{
// create the ProcessStartInfo using "cmd" as the program to be run, and "/c " as the parameters.
// Incidentally, /c tells cmd that we want it to execute the command that follows, and then exit.
System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo(workingDirectory + "\\" + "my.bat ", command);
procStartInfo.WorkingDirectory = workingDirectory;
//This means that it will be redirected to the Process.StandardOutput StreamReader.
procStartInfo.RedirectStandardOutput = true;
//This means that it will be redirected to the Process.StandardError StreamReader. (same as StdOutput)
procStartInfo.RedirectStandardError = true;
procStartInfo.UseShellExecute = false;
// Do not create the black window.
procStartInfo.CreateNoWindow = true;
// Now we create a process, assign its ProcessStartInfo and start it
System.Diagnostics.Process proc = new System.Diagnostics.Process();
//This is importend, else some Events will not fire!
proc.EnableRaisingEvents = true;
// passing the Startinfo to the process
proc.StartInfo = procStartInfo;
// The given Funktion will be raised if the Process wants to print an output to consol
proc.OutputDataReceived += DoSomething;
// Std Error
proc.ErrorDataReceived += DoSomethingHorrible;
// If Batch File is finished this Event will be raised
proc.Exited += Exited;
}
Algo está apagado, pero lo que usted consigue la idea ...
El HacerAlgo esta función es:
void DoSomething(object sendingProcess, DataReceivedEventArgs outLine);
{
string current = outLine.Data;
}
espero que esto ayude
por 'Bash', ¿quieres decir' Batch'? –
@W_P: bash es un shell unix. –
@Brian: Creo que W_P lo sabe. –