我有一个 C# 程序,我想运行一个外部程序,当程序运行时,它需要读取控制台输出并将其以 JSON 格式发送到服务器。这就是我的想法。它会起作用吗?
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);
}
}