Reputation: 43327
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
Upvotes: 144
Views: 93509
Reputation: 469
For those who happen to end up here some time later:
Those are not the same. But they are mostly the same. So if you got data encoded as "Western European (Windows) 1252", using Latin1 will probably get better results than Ascii or UTF7/8/16.
Check the wiki article for the difference in special characters.
Upvotes: 0
Reputation: 8902
Here is remarks for CodePagesEncodingProvider
:
The .NET Framework for the Windows desktop supports a large set of Unicode and code page encodings. .NET Core, on the other hand, supports only the following encodings:
- ASCII (code page 20127), which is returned by the Encoding.ASCII property.
- ISO-8859-1 (code page 28591).
- UTF-7 (code page 65000), which is returned by the Encoding.UTF7 property.
- UTF-8 (code page 65001), which is returned by the Encoding.UTF8 property.
- UTF-16 and UTF-16LE (code page 1200), which is returned by the Encoding.Unicode property.
- UTF-16BE (code page 1201), which is instantiated by calling the UnicodeEncoding.UnicodeEncoding or UnicodeEncoding.UnicodeEncoding constructor with a bigEndian value of true.
- UTF-32 and UTF-32LE (code page 12000), which is returned by the Encoding.UTF32 property.
- UTF-32BE (code page 12001), which is instantiated by calling an UTF32Encoding constructor that has a bigEndian parameter and providing a value of true in the method call.
Other than code page 20127, code page encodings are not supported. The
CodePagesEncodingProvider
class extendsEncodingProvider
to make these code pages available to .NET Core.
So you need to register encodings provider first to use additional encodings like Windows-1252
.
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
CodePagesEncodingProvider
provides access to an encoding provider for code pages that otherwise are available only in the desktop .NET Framework.
After that you can find more encodings and can get Windows-1252
too:
Encoding win1252 = Encoding.GetEncoding(1252);
Note that you need a reference to System.Text.Encoding.CodePages.dll
to use CodePagesEncodingProvider
in some .net versions you have to add nuget package to your project.
Install-Package System.Text.Encoding.CodePages
Upvotes: 6
Reputation:
Please write:
<ItemGroup>
<PackageReference Include="System.Text.Encoding.CodePages" Version="4.3.0" />
</ItemGroup>
in csproj.
In package console write ' dotnet restore', restore assemblies.
and wite this code for sample:
public class MyClass
{
static MyClass()
{
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
}
}
Upvotes: 17
Reputation: 244988
To do this, you need to register the CodePagesEncodingProvider
instance from the System.Text.Encoding.CodePages
package.
To do that, install the System.Text.Encoding.CodePages package:
dotnet add package System.Text.Encoding.CodePages
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);
Upvotes: 268