Shift-JIS encoding for netstandard library

In net45 , Encoding.GetEncoding("Shift-JIS") works fine, but under netstandard it produces:

System.ArgumentException: "Shift-JIS" is not a supported encoding name. For information on defining custom encoding, see the documentation for the Encoding.RegisterProvider method.

The documentation referenced mentions support through CodePagesEncodingProvider for .NET Core Native under UWP, but nothing to do with netstandard .

So, is it possible to use Shift-JIS encoding in the netstandard library?

+5
source share
1 answer

Yes it is possible. Link System.Text.Encoding.CodePages package in project.json :

 "System.Text.Encoding.CodePages": "4.0.1" 

Call the following code before , getting the Shift-JIS encoding:

 Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); 

And you will be fine:

 Encoding.GetEncoding("Shift-JIS") 

UPDATE:

System.Text.Encoding.CodePages is not included in the NETStandard.Library 1.6 package , but there is no problem linking System.Text.Encoding.CodePages from your netstandard class netstandard (until your class library targets netstandard1.2 or lower).

Below is a sample solution with a code . There is a class library that targets netstandard1.3 and a console application application that targets netcoreapp1.0 and references the class library. The class library contains code corresponding to the extraction of the Shift-JIS encoding. It can also be referenced and used by applications designed for other frameworks.

+4
source

Source: https://habr.com/ru/post/1259009/


All Articles