我有一个控制台应用程序。我想记录控制台窗口中显示的所有信息,包括我输入的命令和从该应用程序返回的信息。怎么做?我正在使用C#语言。
答案 0 :(得分:1)
您可以创建一个字符串列表,并将每个提示和响应添加到该列表。从那里,您可以将录制内容写入控制台,将其写入文件或其他内容。
namespace RecordConsoleProgress
{
using System.Collections.Generic;
using static System.Console;
class Program
{
static List<string> recording = new List<string>();
static void Main(string[] args)
{
string response = "";
do
{
string prompt = "Please enter your input (Q <ENTER> to Quit): ";
Write(prompt);
recording.Add(prompt);
response = ReadLine();
recording.Add(response);
} while (response != "Q");
WriteLine("\nYour recording follows");
foreach (string s in recording)
WriteLine(s);
ReadLine();
}
}
}