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)
{
    Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

    // 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.

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

    • Pingback:The Morning Brew - Chris Alcock » The Morning Brew #2436

    • November 13, 2022 at 4:21 pm
      Permalink

      Ido not understand the explanation, because these explanations are clearly meant for c and not the basic variant of the system.

      Equivalent VB code would be nice as wall as instruction where to place the précisely to place the code.

      Greetings

      Marc

    • June 19, 2025 at 3:47 am
      Permalink

      What a material of un-ambiguity and preserveness of precious knowledge on the topic of unpredicted emotions.|

    • June 23, 2025 at 5:54 am
      Permalink

      I’m really loving the theme/design of your website. Do you ever run into any internet browser compatibility issues? A handful of my blog readers have complained about my blog not working correctly in Explorer but looks great in Opera. Do you have any suggestions to help fix this issue?|

    • July 7, 2025 at 3:47 pm
      Permalink

      Fantastic beat ! I would like to apprentice while you amend your site, how can i subscribe for a blog site? The account helped me a acceptable deal. I had been a little bit acquainted of this your broadcast provided bright clear concept|

    • July 20, 2025 at 7:20 pm
      Permalink

      Howdy! I know this is somewhat off topic but I was wondering which blog platform
      are you using for this website? I’m getting sick and tired of WordPress because I’ve had issues with
      hackers and I’m looking at alternatives for another platform.
      I would be awesome if you could point me in the direction of a good platform.

    • July 28, 2025 at 4:37 pm
      Permalink

      The top escorts in Islamabad offer a luxurious and discreet experience that caters to your specific needs. Whether you’re attending an event or simply want to unwind, they provide services that are tailored to your desires.|

    Leave a Reply

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