Как вывести все, что есть в консоли - C#
Формулировка задачи:
Вывел я в консоль "Hello World", а потом "CyberForum". Могу ли я получить в виде строки все, что я вывел (и еще желательно ввел)?
Решение задачи: «Как вывести все, что есть в консоли»
textual
Листинг программы
- class Program {
- class ConsoleStream : TextWriter {
- private TextWriter _inner;
- private string _content;
- public override Encoding Encoding => _inner.Encoding;
- public ConsoleStream(TextWriter inner) {
- _inner = inner;
- _content = string.Empty;
- }
- public override string ToString() {
- return _content;
- }
- private void WriteInternal(string value) {
- _inner.Write(value);
- _content += value;
- }
- public override void WriteLine() {
- WriteInternal(NewLine);
- }
- public override void Write(string value) {
- WriteInternal(value);
- }
- public override void WriteLine(string value) {
- Write(value);
- WriteLine();
- }
- }
- static void Main(string[] args) {
- var cs = new ConsoleStream(Console.Out);
- Console.SetOut(cs);
- for (int i = 0; i < 10; i++) {
- Console.WriteLine(i);
- Task.Delay(100).Wait();
- }
- Console.WriteLine("------");
- Console.Write(cs.ToString());
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д