Attention !

Ce site n'est plus maintenu. Retrouvez l'article sur Colorseeds.net

Vous allez être redirigé dans 5 secondes...

Vous êtes ici : Accueil > Blog technique > C# : Exécut

C# : Exécuter un batch et récupérer la sortie.
02/10/2008


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);
}

Ajouter un commentaire

Auteur


Email


Message *