Skip to content

Instantly share code, notes, and snippets.

@alxsimo
Created May 13, 2014 18:30
Show Gist options
  • Save alxsimo/19b9a23ba97227fabf58 to your computer and use it in GitHub Desktop.
Save alxsimo/19b9a23ba97227fabf58 to your computer and use it in GitHub Desktop.
[C#] Lanzar proceso en background y recoger resultado
//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