【发布时间】:2021-08-27 17:42:34
【问题描述】:
我正在尝试重定向 dotnet-counter 的输出以显示到 Web 应用程序,但是抛出了无效异常,这是我的代码:
var startinfo = new ProcessStartInfo( "dotnet-counters" )
{
Arguments = $"monitor --process-id 1",
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
RedirectStandardInput = true,
};
var process = new Process { StartInfo = startinfo };
process.OutputDataReceived += OnReceived;
process.ErrorDataReceived += OnReceived;
process.Exited += OnExist;
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
当进程启动时,从标准输出中读取一条错误消息:
未处理的异常:System.InvalidOperationException:当任一应用程序没有控制台或控制台输入已从文件重定向时,无法查看是否按下了键。试试 Console.In.Peek
似乎标准输出无法以这种方式重定向,有什么想法吗?
【问题讨论】:
-
你问的是重定向输出;但错误是关于输入。去掉
ReadLine()函数还能用吗? -
@Felix 如果 BeginOutputReadLine 和 BeginErrorReadLine 被删除,输出不再可用
-
等待 - 您的变量是
process,但您正在调用类Process上的函数。你不会得到编译错误吗? (我愿意)