Capturing output from standard io encoding?

3.2k Views Asked by At

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?

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);

Which goves the incorrect result.

edit: code:

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();
1

There are 1 best solutions below

0
On

Found the solution. You can set

   processStartInfo.StandardOutputEncoding = Encoding.UTF8;

This makes it output correctly.