28

I need to encode some text files for native characters.

In my Windows 8.1 Store app, I could use Encoding.GetEncoding() method normally:

Encoding.GetEncoding("windows-1254")

But in UWP app, I got this exception:

Additional information: 'windows-1254' is not a supported encoding name. For information on defining a custom encoding, see the documentation for the Encoding.RegisterProvider method.

I don't know how to make it work by using Encoding.RegisterProvider method, any ideas?

ZygD
  • 22,092
  • 39
  • 79
  • 102

1 Answers1

64

We need to use the CodePagesEncodingProvider to register extended encodings included in that specific provider. See CodePagesEncodingProvider Class

Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
Encoding.GetEncoding("windows-1254");

Ref https://msdn.microsoft.com/en-us/library/system.text.encodingprovider(v=vs.110).aspx

The .NET Framework Class Library provides one static property, P:System.Text.CodePagesEncodingProvider.Instance, that returns an EncodingProvider object that makes the full set of encodings available on the desktop .NET Framework Class Library available to .NET Core applications.

The related thread in MSDN forum: Encoding.RegisterProvider -- How to use?

Franklin Chen - MSFT
  • 4,845
  • 2
  • 17
  • 28
  • Interesting, don't know this change, I will test it later, have you tried that in UWP app? –  Nov 07 '15 at 06:12
  • 3
    Surely you ought to mention that this can only work on a UWP app that runs on a desktop? – Hans Passant Nov 07 '15 at 07:49
  • 2
    In order to use this you need this nuget package: `System.Text.Encoding.CodePages`. And you need to target UWP, of course. I am not sure if that works on all devices, but maybe it doesn't, as the previous comments suggest... – NoOne Oct 21 '17 at 20:24