138

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
1
  • This is not a real fix because it changes the code in the question. But if the reason to use codepage 1252 is reading/writing characters of ISO-8859-1 then one could replace it by 28591 which is included in .NET Core without adding a CodePages package: learn.microsoft.com/en-us/dotnet/api/… Be aware that some characters beyond ISO-8859-1are different in codepage 1252 en.wikipedia.org/wiki/ISO/IEC_8859-1#Windows-1252, especially the Euro sign (€).
    – stb
    Jan 16, 2020 at 9:22

3 Answers 3

252

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);
6
  • 1
    Where should I install or where directory should I run the dotnet add package System.Text.Encoding.CodePages?
    – Rich
    May 22, 2018 at 12:55
  • 2
    @Rich You should run it from the directory with your csproj. If you're using Visual Studio, you can also use the Package Manager instead of that command.
    – svick
    May 23, 2018 at 10:23
  • The above won't help with .NET Core 2.0+ for some particular Encoding properties like HeaderName. This will still throw NotSupportedException.
    – Alex
    Apr 5, 2019 at 16:58
  • 2
    BTW: One should not use CodePagesEncodingProvider.Instance.GetEncoding with a variable input. learn.microsoft.com/en-us/dotnet/api/… says: "you should not call the EncodingProvider.GetEncoding overloads". Reason might be that EncodingProvider will not give you every encoding, but probably only additional encodings (e.g. you won't get ISO-8859-1 codepage as CodePagesEncodingProvider.Instance.GetEncoding(28591) gives null).
    – stb
    Jan 16, 2020 at 9:27
  • In Azure Functions on .net 6 this line throws an exception FileNotFoundException: Could not load file or assembly 'System.Threading, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.. Seems like nobody has ever had this problem before. Any ideas?
    – Tom W
    Mar 2 at 17:09
16

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);
    }
}
4
  • 1
    The NET CORE version for this question is 1.0 (as should be obvious from the date); this answer doesn't work.
    – Joshua
    Sep 6, 2017 at 15:17
  • I know it was the wrong version (this worked for me in 1.1, but it worked like a charm. Gracias amigo.
    – Eric
    Sep 8, 2017 at 23:19
  • 1
    I think it's important to add that the static constructor won't work if your encoding is stored as a static readonly field or static const on the same class. It won't be executed in time when accessing the field. One way around this is to use a lambda property: public static Encoding Windows1252 => Encoding.GetEncoding(1252);
    – masterwok
    Jan 16, 2019 at 3:56
  • 1
    @masterwok: You can't store one of these in a const and if you want it in a static readonly you can initialize it in the static constructor rather than inline.
    – Joshua
    Jan 18, 2021 at 16:50
3

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 extends EncodingProvider 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
1
  • Late, fat, and adds nothing to existing answers.
    – Joshua
    Mar 21 at 16:06

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.