我有两个控制台应用程序,ConsoleApp1和ConsoleApp2。 ConsoleApp2将读取ConsoleApp1的标准输出,并将其显示在控制台中。我不明白为什么如果targetframework是net462还是可以正常工作,但是如果是netcoreapp3.1则不能正常工作。读取标准输出的ConsoleApp2的代码是:
using System;
using System.Diagnostics;
using System.Text;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
CaptureStOut();
}
private static void CaptureStOut()
{
var processStartInfo = new ProcessStartInfo();
var messagesBuilder = new StringBuilder();
using (var process = new Process())
{
processStartInfo.FileName = "ConsoleApp1.exe";
processStartInfo.CreateNoWindow = true;
processStartInfo.UseShellExecute = false;
processStartInfo.RedirectStandardOutput = true;
process.OutputDataReceived += (sender, e) => messagesBuilder.Append(e.Data);
process.StartInfo = processStartInfo;
process.Start();
process.BeginOutputReadLine();
process.WaitForExit();
process.CancelOutputRead();
var message = messagesBuilder.ToString();
if (!string.IsNullOrEmpty(message))
{
Console.WriteLine("Data Received: " + message);
}
else
{
Console.WriteLine("No Data Received");
}
}
}
}
}