在net45中,Encoding.GetEncoding("Shift-JIS")工作得很好,但在netstandard下它会抛出:
System.ArgumentException:“Shift-JIS”不是受支持的编码名称。有关定义自定义编码的信息,请参阅Encoding.RegisterProvider方法的文档。
它引用的文档提到了在UWP下通过CodePagesEncodingProvider对.NET核心原生的支持,但没有对netstandard的一般使用提供支持。
那么,在netstandard库中使用Shift-JIS编码是可能的吗?
发布于 2016-11-11 21:44:49
是的,这是可能的。在project.json中引用System.Text.Encoding.CodePages包
"System.Text.Encoding.CodePages": "4.0.1"在获取Shift-JIS编码之前,调用以下代码:
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);你很适合这样做:
Encoding.GetEncoding("Shift-JIS")更新:
System.Text.Encoding.CodePages没有捆绑在NETStandard.Library 1.6 package中,但是从您的netstandard类库引用System.Text.Encoding.CodePages是没有问题的(直到您的类库以netstandard1.2或更低版本为目标)。
这是一个sample solution with the code。有一个面向netstandard1.3的类库和一个面向netcoreapp1.0并引用该类库的消费控制台应用程序。类库包含与Shift-JIS编码检索对应的代码。它也可以从针对其他框架的应用程序中引用和使用。
https://stackoverflow.com/questions/40331957
复制相似问题