Encoding.GetEncoding can't work in UWP app

user3977385 picture user3977385 · Nov 7, 2015 · Viewed 10.9k times · Source

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?

Answer

Franklin Chen - MSFT picture Franklin Chen - MSFT · Nov 7, 2015

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?