C# : Exécuter un batch et récupérer la sortie.


private int Run_Batch(string sBatchPath, string sBatchName, string sArgs)
{
	// Code retour du batch
    int iExitCode = 0;
    // Chemin complet du batch
    string sBatchFullPath = sBatchPath + "\" + sBatchName;
    // Configuration du batch (chemin, arguments, sorties...)
    ProcessStartInfo processInfo = new ProcessStartInfo(sBatchFullPath);
    processInfo.CreateNoWindow = true;
    processInfo.Arguments = sArgs;
    processInfo.RedirectStandardOutput = true;
    processInfo.RedirectStandardError = true;
    processInfo.UseShellExecute = false;
    // Process en soi
    Process pBatch = new Process();
    // Redirection de la sortie vers une fonction
    pBatch.OutputDataReceived += new DataReceivedEventHandler(pBatch_OutputDataReceived);
    pBatch.StartInfo = processInfo;
    pBatch.EnableRaisingEvents = true;
    // Démarrage du batch + lecture des sorties.
    pBatch.Start();
    pBatch.BeginOutputReadLine();
    pBatch.BeginErrorReadLine();
    // Synchronisation
    pBatch.WaitForExit();
    iExitCode = pBatch.ExitCode;
    // Fermeture
    pBatch.Close();
    return iExitCode;
}
protected void pBatch_OutputDataReceived(Object sender, DataReceivedEventArgs e)
{
    System.Console.Write(e.Data);
}