2013-09-03 14 views
7

Chciałbym przekierować standardowe wyjście procesu do łańcucha w celu późniejszego przeanalizowania. Chciałbym również zobaczyć dane wyjściowe na ekranie, gdy proces jest uruchomiony, i to nie tylko po jego zakończeniu.Przekierowanie wyjściowe procesu C#

Czy to możliwe?

+0

Proszę podać więcej szczegółów na temat tego, co faktycznie próbujesz wykonać. – Patel

+0

możliwy duplikat [wyjścia procesu C# podczas działania] (http://stackoverflow.com/questions/11994610/c-sharp-get-process-output- podczas biegu) –

Odpowiedz

15

Użyj RedirectStandardOutput.

Próbka z 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(); 

zobaczyć również OutputDataReceived i BeginOutputReadLine() alternatywy dla ReadToEnd(), że będzie lepiej wypełniać swoje „widzi wyjście, gdy proces jest uruchomiony” wymóg.

0

Jeśli chcesz wykonać exe od aplikacji C# i uzyskać wyjście z niego można użyć poniższy kod

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

Nie forgete napisać p.EnableRaisingEvents = true;

Powiązane problemy