views:

424

answers:

1

I am writing a program in dot net that will execute scripts and command line programs using the framework 2.0's Process object. I want to be able to access the screen buffers of the process in my program. I've investigated this and it appears that I need to access console stdout and stderr buffers. Anyone know how this is accomplished using managed code?

I think I need to use the AttachConsole and the ReadConsoleOutput of the windows console attached to the task in order to read a block of character and attribute data from the console screen. I need to do this is managed code.

see http://msdn.microsoft.com/en-us/library/ms684965(VS.85).aspx

+2  A: 

You can accomplish this using the StandardError, StandardOutput, and StandardInput properties on the System.Diagnostics.Process class.

MSDN has a nice example of redirecting standard in and out of a process.

Note that you can only redirect the output of processes that you started. External processes that you didn't launch can't have their stdout redirected after the fact.

Also note that to use StandardInput, you must set ProcessStartInfo.UseShellExecute to false, and you must set ProcessStartInfo.RedirectStandardInput to true. Otherwise, writing to the StandardInput stream throws an exception.

Judah Himango
Thanks for the swift response. I don't want to read the stream however. I want to access the console buffer which is displayed in the command window. It would be similar to doing screen scraping on a html page.
William Main
I think I need to use the AttachConsole method of the console then the ReadConsoleOutput method to access a rectangle of character and attribute data. I don't know how to do this in managed code.
William Main