gpt4 book ai didi

c# - ProcessStartInfo - 在控制台窗口中打印输出并输出到文件 (C#)

转载 作者:行者123 更新时间:2023-11-30 22:29:03 26 4
gpt4 key购买 nike

在我的 C# 应用程序中,我使用以下 ProcessStartInfo 调用 Process.Start(myPSI):

ProcessStartInfo startInfoSigner = new ProcessStartInfo();
startInfoSigner.CreateNoWindow = false;
startInfoSigner.UseShellExecute = false;
startInfoSigner.FileName = pathToMyEXE;
startInfoSigner.WindowStyle = ProcessWindowStyle.Hidden;
startInfoSigner.WindowStyle = ProcessWindowStyle.Minimized;

startInfoSigner.RedirectStandardOutput = true;

这会在运行应用程序时打开一个新的控制台窗口,并且不会产生任何输出(因为它已被重定向)。我读取 exe 进程标准输出并将其写入文件。

有没有办法仍然在这个新的控制台窗口中显示信息,并将其写入文件(不修改 pathToMyEXE 可执行文件)?

最佳答案

您需要 RedirectStandardOutput = true 才能处理 OutputDataReceived事件。在事件处理程序中执行日志记录并将数据写回控制台。

private void OutputDataReceived(object sender, DataReceivedEventArgs e) 
{
logger.log(someString);//write to the file
Console.WriteLine(e.Data);//still display the info in this new console window
}

关于c# - ProcessStartInfo - 在控制台窗口中打印输出并输出到文件 (C#),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10389119/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com