.NET Core 不知道 Windows 1252,如何解决? [英] .NET Core doesn't know about Windows 1252, how to fix?

查看:26
本文介绍了.NET Core 不知道 Windows 1252,如何解决?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

为此,您需要 注册 CodePagesEncodingProvider 来自 System.Text.Encoding.CodePages 包的实例.

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

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

dotnet add package System.Text.Encoding.CodePages

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

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

这篇关于.NET Core 不知道 Windows 1252,如何解决?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆