NotSupportedException: No data is available for encoding 1252

Another day, another surprise on .NET Core. Tried to load a file with Windows-1252 encoding and got the following exception: NotSupportedException: No data is available for encoding 1252. For information on defining a custom encoding, see the documentation for the Encoding.RegisterProvider method. Here’s how to solve the problem.

Adding missing encodings

It turns out that some “exotic” encodings (including popular Windows-1252) are defined in separate NuGet package and these encodings are not available by default.

Solution is simple. Add System.Text.Encoding.CodePages NuGet package to solution and use the following piece of code in application startup class to registester new encodings.

public void ConfigureServices(IServiceCollection services)

    // more code here

Changing encoding of text

Now let’s convert file in Windows-1252 encoding to UTF-8.

var path = "c:\\temp\\observations.txt";

var estEncoding = Encoding.GetEncoding(1252);
var est= File.ReadAllText(path, estEncoding);                             
var utf = Encoding.UTF8;
est = utf.GetString(Encoding.Convert(estEncoding, utf, estEncoding.GetBytes(est)));

Important thing in the code above is using Windows-1252 encoding when reading contents of file. With out specifying encoding it is expected that file is in UTF-8 already.

Gunnar Peipman

Gunnar Peipman is ASP.NET, Azure and SharePoint fan, Estonian Microsoft user group leader, blogger, conference speaker, teacher, and tech maniac. Since 2008 he is Microsoft MVP specialized on ASP.NET.

    3 thoughts on “NotSupportedException: No data is available for encoding 1252

    Leave a Reply

    Your email address will not be published. Required fields are marked *