1

I have a WPF application that also writes to stdout because it can operate in two modes, UI or console. The output contains non-ascii characters, e.g.

Console.WriteLine("ÀÁÂÃÄÅÆ ®¯°±¹²³¼½¾¶·")

The result is a stream that I can redirect into a file and the file is encoded as ISO-8859 but I want it to be UTF-8.
For console applications this answer suggests to set

Console.OutputEncoding = System.Text.Encoding.UTF8;

But when I do that in my application I get

System.IO.IOException "The handle is invalid" when setting OutputEncoding
user829755
  • 1,489
  • 13
  • 27
  • There is a mix of Latin and Greek characters. How those could be both concurrently encoded as `iso-8859-1` (Latin) and `iso-8859-7` (Greek) in a single `Console.WriteLine` command? Please [edit] your question to provide a [mcve]. – JosefZ Sep 28 '22 at 14:39
  • you're right the greek letters are replaced with latin characters that look similar. I'll remove them because this is not the point. Anyway when I run the result through the linux `file` command I get "ISO-8859 text, with CRLF line terminators", i.e. not "ISO-8859-1". What I want is UTF-8 and that does allow a mix of latin and greek letters, doesn't it? Note that my source code is UTF-8, too. – user829755 Sep 28 '22 at 15:06
  • 1
    That's why I have set _[Beta: Use Unicode UTF-8 for worldwide language support](https://superuser.com/a/1451686/376602)_, following [UTF-8 Everywhere](https://utf8everywhere.org/). – JosefZ Sep 28 '22 at 15:27
  • @JosefZ, that helped! thank you :-) If you add it as answer I can accept it. – user829755 Oct 01 '22 at 11:25
  • Please [post your own answer](https://stackoverflow.com/help/self-answer) so that others can find the solution. – JosefZ Oct 01 '22 at 15:11

2 Answers2

0

as pointed out by JosefZ:
There's a checkbox since Windows 10 insider build 17035 for setting the locale code page to UTF-8:

Control Panel > Cloc and Region > Region > Administrative > Change system locale... > Beta: Use Unicode UTF-8 for worldwide language support = true

see here for more details

user829755
  • 1,489
  • 13
  • 27
0

This is the weirdest thing Microsoft made me do for Visual Studio 2019 and you have to REBOOT your PC :)

enter image description here

Meryan
  • 1,285
  • 12
  • 25