How to enable encoding code page 1252 in dotnet core?

I know that dotnet core is still in development, i.e. not all .net 4.5 functionality is supported out of the box for a dotnet core app. But I was really surprised to find out that Encoding.GetEncoding(1252) was returning a null value! I even tried to be smart: Encoding.GetEncoding(“Latin 1”), nope, not working either …

So what do you do when you do not know the answer? You ask your friend Google 😉 But my Google fu did not award any result, so I resorted to Twitter. Maybe the dotnet core devs could help me out?


Ah, great! Immo sent me to the CodePagesEncodingProvider Class.
Thanks a lot, man!

In project.json I added a dependency to System.Text.Encoding.CodePages, like,

    "frameworks": {
        "netcoreapp1.1": {
          "dependencies": {
            "System.Text.Encoding.CodePages": "4.3.0",
              

And in the code, I registered the instance before calling any Encoding.GetEncoding(1252)

    public static void Main(string[] args)
    {
        Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
              

After these two changes and calling dotnet restore, the code page 1252 was working in the dotnet core app, yay!

// Ryan

Author:

Ryan Heath

Email:

ryan@kancane.nl

Date:

January 9th, 2017

At:

Hoorn, The Netherlands