Reputation: 21
I am currently in the process of making a server/client chat program as a console app in C#. I am working on commands (/disconnect, /about, etc.) and I want to make a /savelog command.
My question is, how can I write the entire console output to a .txt file? I already know how to call the function when the command is detected, but how can I actually do the work?
Ex:
'>>Connection established
'>>Message: hi
'>>(Server)Message: hello there
'>>Message: /savelog
'>>Message: SAVED TO dir
Upvotes: 2
Views: 192
Reputation: 16584
The .NET framework doesn't contain a mechanism for reading the contents of the console window. If you really need to do this, you will need to use the Windows APIs directly.
The specific API function you need is ReadConsoleOutputCharacter
which will copy blocks of characters from the console buffer to an array. There is a good example of how to use this on PInvoke.Net.
However... if you need logging, it should be done at the time the data is being written to the screen. Relying on the console state is not necessarily sufficient, as there are various things that can result in the console buffer having unexpected data in it.
Upvotes: 0