vb.net .NET Core 不知道 Windows 1252,如何解决?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37870084/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:12:49  来源:igfitidea点击:

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

vb.netcharacter-encoding.net-corewindows-1252

提问by Joshua

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

这个程序在为 .NET 4 编译时工作得很好,但在为 .NET Core 编译时就可以了。我了解有关不支持编码的错误,但不了解如何修复它。

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

回答by svick

To do this, you need to registerthe CodePagesEncodingProviderinstance from the System.Text.Encoding.CodePagespackage.

要做到这一点,你需要注册CodePagesEncodingProvider从实例System.Text.Encoding.CodePages包。

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

为此,请安装System.Text.Encoding.CodePages 包

dotnet add package System.Text.Encoding.CodePages

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

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

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);

回答by Joshua

Please write:

请写出:

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

in csproj.

在 csproj 中。

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

在包控制台中写入“dotnet restore”,还原程序集。

and wite this code for sample:

并使用此代码作为示例:

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