2013-09-03 5 views
7

Je souhaite rediriger la sortie standard du processus vers une chaîne pour l'analyse ultérieure. Je voudrais également voir la sortie sur l'écran, pendant que le processus est en cours, et pas seulement quand il est terminé.Sortie du processus de redirection C#

Est-ce encore possible?

+0

Veuillez fournir plus de détails sur ce que vous essayez réellement de faire – Patel

+0

duplication possible de [C# obtenir la sortie du processus en cours d'exécution] (http://stackoverflow.com/questions/11994610/c-sharp-get-process-output- en courant) –

Répondre

15

Utilisez RedirectStandardOutput.

Exemple de MSDN:

// Start the child process. 
Process p = new Process(); 
// Redirect the output stream of the child process. 
p.StartInfo.UseShellExecute = false; 
p.StartInfo.RedirectStandardOutput = true; 
p.StartInfo.FileName = "Write500Lines.exe"; 
p.Start(); 
// Do not wait for the child process to exit before 
// reading to the end of its redirected stream. 
// p.WaitForExit(); 
// Read the output stream first and then wait. 
string output = p.StandardOutput.ReadToEnd(); 
p.WaitForExit(); 

Voir aussi OutputDataReceived et BeginOutputReadLine() une alternative à ReadToEnd(), qui permettra de mieux répondre à votre « voir la sortie alors que le processus est en cours d'exécution » exigence.

0

Si vous voulez exécuter un fichier EXE de votre application C# et obtenir la sortie de celui-ci, vous pouvez utiliser le code ci-dessous

System.Diagnostics.Process p = new System.Diagnostics.Process();    

p.StartInfo.FileName = "PATH TO YOUR FILE"; 
p.StartInfo.UseShellExecute = false; 
p.StartInfo.Arguments = metalType + " " + graphHeight + " " + graphWidth; 
p.StartInfo.CreateNoWindow = true; 
p.StartInfo.RedirectStandardOutput = true; 
p.StartInfo.RedirectStandardError = true;    

p.EnableRaisingEvents = true; 
p.Start();    
svgText = p.StandardOutput.ReadToEnd(); 

using(StreamReader s = p.StandardError) 
{ 
    string error = s.ReadToEnd(); 
    p.WaitForExit(20000); 
} 

Ne pas forgete écrire p.EnableRaisingEvents = true;

Questions connexes