.NET Core doesn't know about Windows 1252, how to fix?

vb.netCharacter Encoding.Net CoreWindows 1252

vb.net Problem Overview


This program works just fine when compiled for .NET 4 but does not when compiled for .NET Core. I understand the error about encoding not supported but not how to fix it.

Public Class Program
    Public Shared Function Main(ByVal args As String()) As Integer
        System.Text.Encoding.GetEncoding(1252)
    End Function
End Class

vb.net Solutions


Solution 1 - vb.net

To do this, you need to register the CodePagesEncodingProvider instance from the System.Text.Encoding.CodePages package.

To do that, install the System.Text.Encoding.CodePages package:

dotnet add package System.Text.Encoding.CodePages

Then (after implicitly or explicitly running dotnet restore) you can call:

Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
var enc1252 = Encoding.GetEncoding(1252);

Alternatively, if you only need that one code page, you can get it directly, without registration:

var enc1252 = CodePagesEncodingProvider.Instance.GetEncoding(1252);

Solution 2 - vb.net

Please write:

<ItemGroup>
    <PackageReference Include="System.Text.Encoding.CodePages" Version="4.3.0" />
</ItemGroup>

in csproj.

In package console write ' dotnet restore', restore assemblies.

and wite this code for sample:

public class MyClass
{
    static MyClass()
    {
        Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
    }
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJoshuaView Question on Stackoverflow
Solution 1 - vb.netsvickView Answer on Stackoverflow
Solution 2 - vb.netuser6845177View Answer on Stackoverflow