捕获标准io编码的输出

本文关键字:输出 编码 io 标准 | 更新日期: 2023-09-27 18:26:45

我正在将应用程序的输出管道传输到我的.NET应用程序中。

编码有些奇怪。字母ÅõÖ显示为├ñ├ñ├

我试着从各种不同的编码中来回转换,但没有成功。有人知道这里应该如何正确转换字符串吗?

例如,应用程序的文档说输出是UTF8,所以我尝试了这个:

byte[] encodedBytes = Encoding.UTF8.GetBytes(theOutput);
var res = Encoding.Default.GetString(encodedBytes);

这是错误的结果。

编辑:代码:

var processStartInfo = new ProcessStartInfo
{
   CreateNoWindow = true,
   RedirectStandardOutput = true,
   RedirectStandardInput = true,
   UseShellExecute = false,
   Arguments = a,
   FileName = path + "''phantomjs.exe"
};
var process = new Process
{
   StartInfo = processStartInfo,
   EnableRaisingEvents = true
};
//capturing output here
process.OutputDataReceived += 
   (sender, args) => outputBuilder.Append(args.Data);
process.Start();
process.BeginOutputReadLine();
process.WaitForExit(20000);
process.CancelOutputRead();

捕获标准io编码的输出

找到了解决方案。您可以设置

   processStartInfo.StandardOutputEncoding = Encoding.UTF8;

这使其输出正确。