6

I'm trying to capture output from a console application by running it in a test using System.Diagnostics.Process. I'm having trouble with character encoding. "£" is showing up as "œ" in the test, but when I run the console application it displays correctly as "£".

If I set Console.Out.Encoding = Encoding.Default, it works in the tests but doesn't display properly when running normally.

What's going on here and how do I fix it?

JontyMC
  • 6,038
  • 6
  • 38
  • 39

2 Answers2

5

You need to set the StandardOutputEncoding on your ProcessStartInfo object in your test case:

var process = new Process();
var startInfo = new ProcessStartInfo(@"yourapp.exe");
startInfo.StandardOutputEncoding = Encoding.GetEncoding(850);

You can find what CodePage you are using in your console app by running

Console.WriteLine(Console.Out.Encoding.CodePage); 

which returns 850 (Western European DOS)

You could also use the BodyName property as an arg to GetEncoding that is:

startInfo.StandardOutputEncoding = Encoding.GetEncoding("ibm850");
wal
  • 17,409
  • 8
  • 74
  • 109
  • I've come across this issue. I'm redirecting output to a file and running from a cmd prompt but I would like output to remain encoded in UTF-8. Is there a way to set this in a command window? as I'm not starting the process via another C# application. Thanks – Chris Walsh Mar 05 '15 at 13:50
  • 1
    @ChrisWalsh see http://stackoverflow.com/questions/388490/unicode-characters-in-windows-command-line-how ? `chcp 65001` – wal Mar 05 '15 at 23:36
1

I think you are on the right track. As a test I executed this code:

Console.WriteLine(Encoding.Default.EncodingName);           
Console.WriteLine(Console.Out.Encoding.EncodingName);   

When run as a console application it outputs:

Western European (Windows)
OEM United States

When run as a windows application it outputs:

Western European (Windows)
Western European (Windows)

I suspect that when reading from the console output from the test runner you will need to set the encoding of your reader to match the encoding that the output is written in, probably Western European (Windows), though I can't know for sure.

MarkPflug
  • 28,292
  • 8
  • 46
  • 54