1

I've observed, using Visual studio 2008 (with .NET 3.5), that the the value of Console.Out.Encoding changes based upon if running via a debugger and Console App/Windows App in the following way:

  • GUI App - using debugger
    • System.Text.SBCSCodePageEncoding
  • GUI App - not using a debugger
    • System.Text.UTF8Encoding
  • Console App - using a debugger
    • System.Text.SBCSCodePageEncoding
  • Console App - not using a debugger
    • System.Text.SBCSCodePageEncoding

Now when Console.Out.Encoding is set to SBCSCodePageEncoding it doesn't throw exceptions when invalid utf16 surrogates written to it. For example:

string invalidStringContiaingHighOrderSurrogateWithOutMatchingLowOrderSurrogate = '\uD81B'.ToString() + ";";
Console.WriteLine(invalidStringContiaingHighOrderSurrogateWithOutMatchingLowOrderSurrogate);
Console.Out.Flush();

However when Console.Out.Encoding is set to System.Text.UTF8Encoding writing invalid utf16 surrogates to it throws System.Text.EncoderFallbackException.

I want to be a able to ignore EncoderFallbackExceptions without having to add a try catch block to each use of Console.Out in the large windows application I working with. How can I do this?

  1. Changing to a console app is NOT an acceptable solution.
  2. Somehow changing Console.Out.Encoding to SBCSCodePageEncoding IS a acceptable solution.
  3. I can't change Console.Out.Encoding.EncoderFallback as I get the following exception System.InvalidOperationException "Instance is readonly"
  4. The win32 function SetConsoleOutputCP seemed to have no effect on my GUI/Windows app.
Tom
  • 6,325
  • 4
  • 31
  • 55

1 Answers1

1

Use the Console.OutputEncoding property to set the encoding however you want.

(Personally I'd stick with UTF-8, but create an UTF8Encoding, setting an appropriate EncoderFallback value, e.g. ReplacementFallback.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Doesn't that only work for Console apps or am I doing somthing wrong? Console.OutputEncoding = new UTF8Encoding(); throws IOException "the handle is invalid" for Windows/GUI applications. – Tom Sep 02 '13 at 20:13
  • That's very odd. I guess it depends on what you're expecting to do with the console output in a GUI app to start with. Do you just want it in the output window in VS? – Jon Skeet Sep 02 '13 at 20:23
  • Yes when running in a debugger the output is intended for VS (Output/Debug window or intermediate window depending on developers settings) - I guess I could just add IsDebuggerAttached calls before the Console.WriteLines but that's only marginally better than wrapping them in try catch blocks. – Tom Sep 02 '13 at 20:50
  • Note that changes to encoding in `Console` were made in both .NET 4.0 and .NET 4.5 (also these versions are mentioned on the first page you link, in _Remarks_ section). The poster uses .NET 3.5, I think. Not sure if this is relevant though. – Jeppe Stig Nielsen Sep 02 '13 at 21:01
  • Yep - using .NET 3.5 I will update the question to make that clear. Thanks – Tom Sep 02 '13 at 21:04