Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Screen scraping a command window using .net managed code

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

like image 647
William Main Avatar asked Nov 05 '22 23:11

William Main


1 Answers

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.

like image 50
Judah Gabriel Himango Avatar answered Nov 18 '22 12:11

Judah Gabriel Himango