How to redirect the process result to console.  
Author Message
Prabu.





PostPosted: Visual C# General, How to redirect the process result to console. Top

Hi,

I would like to redirect the process output the console and write it into the file. I am using the following code. But it doesn’t work. Please let me know what I missed here.

string test = string.Format ( "\"{0}\" /build debug " , file.FullName , d.FullName + "\\devenv.log" );

Process projectProcess = new Process( );

projectProcess.StartInfo = new ProcessStartInfo( devLoc + "devenv.exe" , test );

Console.WriteLine ( file.FullName );

projectProcess.StartInfo.CreateNoWindow = true;

projectProcess.StartInfo.UseShellExecute = false;

projectProcess.StartInfo.ErrorDialog = true;

projectProcess.StartInfo.RedirectStandardOutput = true;

projectProcess.Start();

string r = projectProcess.StandardOutput.ReadLine();

Console.WriteLine ( r );




Visual C#12  
 
 
TaylorMichaelL





PostPosted: Visual C# General, How to redirect the process result to console. Top

Redirecting output is difficult because you must keep the output buffers from filling up and at the same time not blocking. In v1.x it was really difficult. In v2.0 you should use BeginOutputReadLine to async read the data. The reason is that if a buffer (any buffer) fills up the running process will lock up until it is cleared. Therefore if the running process is writing to both the output and the error streams and your app only reads the output stream then the process will stall if the error stream fills up. However if you are reading synchronously then you'll block if there is no output. The async versions work around this. Refer to the MSDN sample code for BeginOutputReadLine for an example on how to properly read from the output stream.

Michael Taylor - 12/12/06


 
 
Loïc Baumann





PostPosted: Visual C# General, How to redirect the process result to console. Top

I'd like to bounce this topic because I have the same issue. I can display the output of any executable (using the async callbacks), but it simply doesn't work with devenv.exe.

Any idea why

Here's the code :

240 Process devenvprocess = new Process();

241 devenvprocess.StartInfo.FileName = "C:\\Program Files\\Microsoft Visual Studio 8\\Common7\\IDE\\devenv.exe";

242 devenvprocess.StartInfo.Arguments = "solution.sln /build \"debug|win32\"";

243 devenvprocess.StartInfo.UseShellExecute = false;

244 devenvprocess.StartInfo.RedirectStandardOutput = true;

245 devenvprocess.StartInfo.RedirectStandardError = true;

246 devenvprocess.OutputDataReceived += new DataReceivedEventHandler(OutputDataReceived);

247 devenvprocess.ErrorDataReceived += new DataReceivedEventHandler(ErrorDataReceived);

248 bool res = devenvprocess.Start();

249 devenvprocess.BeginOutputReadLine();

250 devenvprocess.BeginErrorReadLine();


 
 
Ernst Kuschke





PostPosted: Visual C# General, How to redirect the process result to console. Top

Instead of doing a ReadLine(), try doing ReadToEnd(); here's an example
http://dotnet.org.za/ernst/archive/2004/04/02/getting-output-from-a-command-prompt.aspx


 
 
Loïc Baumann





PostPosted: Visual C# General, How to redirect the process result to console. Top

It doesn't work too, I already tried. Looks like the matter is more on the Devenv.exe side than the .net side.

Thanks for your answer anyway.

Loic