我有一个C#程序,我想运行一个外部程序,当程序运行时,它需要读取控制台输出并将其以JSON格式发送到服务器。这就是我的想法。它会起作用吗?在程序运行时阅读控制台输出c#
ProcessStartInfo psi = new ProcessStartInfo("app.exe");
psi.RedirectStandardOutput = true;
psi.WindowStyle = ProcessWindowStyle.Hidden;
psi.UseShellExecute = false;
Process app = Process.Start(psi);
while (true)// what do I loop on?
{
string line = "{ \"message\": \"" + app.StandardOutput.ReadLine() + "\" }";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url + "/results/:" + runId + "/logs");
request.ContentType = "text/json";
request.Method = "POST";
using (TextWriter tw = new StreamWriter(request.GetRequestStream()))
{
tw.WriteLine(line);
}
}
好问题。你试过了吗? “它会起作用”是一个模糊的问题 - 我们可能需要更多关于您尝试时会发生什么的细节,以及您期望发生的事情。 –