Created
May 13, 2014 18:30
-
-
Save alxsimo/19b9a23ba97227fabf58 to your computer and use it in GitHub Desktop.
[C#] Lanzar proceso en background y recoger resultado
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
//Lanza un proceso con su ventana oculta y devuelve el resultado | |
private static string lanzaProceso(string Proceso, string Parametros) | |
{ | |
ProcessStartInfo startInfo = new ProcessStartInfo(Proceso, Parametros); | |
startInfo.WindowStyle = ProcessWindowStyle.Hidden; | |
startInfo.UseShellExecute = false; //No utiliza RunDLL32 para lanzarlo | |
//Redirige las salidas y los errores | |
startInfo.RedirectStandardOutput = true; | |
startInfo.RedirectStandardError = true; | |
Process proc = Process.Start(startInfo); //Ejecuta el proceso | |
proc.WaitForExit(); // Espera a que termine el proceso | |
string error = proc.StandardError.ReadToEnd(); | |
if (error != null && error != "") //Error | |
throw new Exception("Se ha producido un error al ejecutar el proceso '" + Proceso + "'\n" + "Detalles:\n" + "Error: " + error); | |
else //Éxito | |
return proc.StandardOutput.ReadToEnd(); //Devuelve el resultado | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment