Can I save the buffer or contents of a console win

2019-03-16 08:43发布

问题:

I am having fun with Console Applications and just wanted to know. What's the best way to save the current buffer of a console window for later use and provide a sort of history navigation function. I know the Diagnostics namespace provides ability to read standard output but that's only if you redirect it, so basically you will have to run process all your results in a new window which seems a little dumb.

回答1:

You need to read the console screen buffer.

I wrote a series of articles about accessing the Console API from .NET a few years ago. The article you're interested in is Working with Console Screen Buffers.

Full source code is available at http://mischel.com/pubs/consoledotnet/consoledotnet.zip.