捕获标准io编码的输出? [英] Capturing output from standard io encoding?
本文介绍了捕获标准io编码的输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在将应用程序的输出传递到我的.NET应用程序中.
I'm piping the output of an app into my .NET app.
编码有点奇怪.字母ÅÄÖ显示为├Ñ├ñ├
The encoding is somewhat strange. Letters ÅÄÖ shows up as ├Ñ ├ñ ├Â
我试图从各种不同的编码来回转换而没有成功.有人知道如何在此处正确转换字符串吗?
I have tried to convert back and forth from various different encodings w/o any success. Anyone know how the string should be converted correctly here?
例如该应用程序的文档说输出为UTF8,所以我已经尝试过了:
e.g. the documentation for the app says the output is UTF8, so I've tried this:
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();
推荐答案
找到了解决方案.您可以设置
Found the solution. You can set
processStartInfo.StandardOutputEncoding = Encoding.UTF8;
这使得它可以正确输出.
This makes it output correctly.
这篇关于捕获标准io编码的输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文