2013-09-03 2 views
7

Я хотел бы перенаправить стандартный вывод процесса в строку для последующего синтаксического анализа. Я также хотел бы видеть вывод на экране, пока процесс выполняется, и не только когда он заканчивается, он запускается.Выход процесса перенаправления C#

Возможно ли это?

+0

Просьба предоставить более подробную информацию о том, что на самом деле вы пытаетесь сделать – Patel

+0

Возможный дубликат [C# get process output while running] (http://stackoverflow.com/questions/11994610/c-sharp-get-process-output- во время бега) –

ответ

15

RedirectStandardOutput.

Пример из 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(); 

Также см OutputDataReceived и BeginOutputReadLine() альтернативу ReadToEnd(), что будет лучше выполнять свой «видеть выход в то время как процесс запущен» требование.

0

Если вы хотите, чтобы выполнить ех из вашего приложения C# и получить на выход из него, то вы можете использовать следующий код

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

Не forgete писать p.EnableRaisingEvents = истина;

Смежные вопросы