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

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
64206 次浏览

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

为此,请安装 系统。文本。编码。代码页包:

dotnet add package System.Text.Encoding.CodePages

然后(在隐式或显式运行 dotnet restore之后)您可以调用:

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

或者,如果你只需要一个代码页,你可以直接得到它,无需注册:

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

请写:

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

在 CSproj。

在包控制台中写入“ dotnet 恢复”,恢复程序集。

并编写此代码作为示例:

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