wrong description
surroundings
- dotnet 2.1.4
phenomenon
When used in code
System.Text.Encoding.GetEncoding("GB2312")
//or
System.Text.Encoding.GetEncoding("GBK")
Will throw an exception:
Unhandled Exception: System.ArgumentException:’GB2312′ is not a supported encoding name. For information on defining a custom encoding, see the documentation for the Encoding.RegisterProvider method.
or
Unhandled Exception: System.ArgumentException:’GBK’ is not a supported encoding name. For information on defining a custom encoding, see the documentation for the Encoding.RegisterProvider method.
solve
the reason
Use the following code to check the supported encoding:
System.Text.Encoding.GetEncodings();
It is found that there is no GB2312 or GBK in the obtained code.
Solution
first step
Add the following packages to the project:
System.Text.Encoding.CodePages
According to the description of System.Text.Encoding.CodePages nuget homepage , this package can provide programs with Windows-1252, Shift-JIS, and GB2312 encodings.
Provides support for code-page based encodings, including Windows-1252, Shift-JIS, and GB2312.
So after importing this package, we will be able to use GB2312 encoding.
In the .csproj
document should add the following code:
<ItemGroup>
<PackageReference Include="System.Text.Encoding.CodePages" Version="4.4.0" />
</ItemGroup>
Or execute the following command in the project directory:
dotnet add package System.Text.Encoding.CodePages --version 4.4.0
Of course, the version number needs to be modified to the latest. At this time (2018.02.22) the latest version is 4.4.0.
Don’t forget to execute dotnet restore
.
Second step
According to error, we need to use coded references Encoding.RegisterProvider
to register functions.
In use System.Text.Encoding.GetEncoding ("GB2312")
prior to execution in the code:
System.Text.Encoding.RegisterProvider (System.Text.CodePagesEncodingProvider.Instance);
After registering, you won’t get an error when you get the GB2312 code object, and you can use the functions normally.
Read More:
- [Solved] net core HTTP Error 500.31 – Failed to load ASP.NET Core runtime HTTP Error 500.30 – ASP.NET Core
- [Solved] Asp.Net Core IIS Error: HTTP Error 500.30 – ASP.NET Core app failed to start
- .Net Core 5.0 Upload File limit via Swagger Api report error: error: request entity too large [Three Methods]
- Net use Errors Explanation
- EF core Creates pg library and Model Report an Error [Solved]
- How to Solve [error] malformed \uxxxx encoding Error
- [Solved] Compile the program error: undefined reference to `cv::dnn::dnn4_v20210301::Net::~Net()‘
- How to parse JSON string in.Net [error reading job object from jsonreader. Current jsonreader item is not an obj]
- PLSQL even oracle11g problem on 64-bit machine: SQL*Net not properly installed and ORA-12154:TNS: cannot handle service name
- Win assembleDebug Downloading gradle.zip Error: Exception in thread “main” java.lang.RuntimeException: javax.net.ssl.SSLExce
- M1 MacBook pod install Report an error chip incompatibility problem
- [Solved] Sparksql error: Exception in thread “main” org.apache.spark.sql.catalyst.errors.package$TreeNodeException
- Bin File Programming Failed, could not find core in coresight setup, erase error
- [Solved] IP Core Map Compile Error: Buffers of the same direction cannot beplaced in series.
- Eclipse start project error: Errors occurred during the build. Errors running builder ‘Integrated External Tool Builder’ on…
- [Solved] Kubernetes Error: failed to list *core.Secret: unable to transform key
- Runtime error prompt in Android Studio: arrayadapter requires the resource ID to be a textview problem
- SLAMBook2 in ch3 code run fatal error: Eigen/Core: No such file or directory
- Spring Connect Redis Error: Error in execution; nested exception is io.lettuce.core.RedisCommandExecutionException: MOVED 12910 172.16.4.99:6379
- Jmeter Error java.net.BindException: Address already in use: connect