Search code examples
c#encodingportable-class-library

How can I determine the default encoding in a portable class library?


With .NET 4.6 I can detect the current codepage with:

System.Text.Encoding.Default

Now I'm trying to port a class library that is reading files from a standard class library to a portable class library, supporting ASP.NET Core 5. With this configuration, Encoding.Default is not availiable.

Of course, any recent file should be saved in UTF-8 encoding, but I can not ensure that all files are stored in this encoding.

Is there another way to get the default codepage?

I looked at the reference source and found that Encoding.Default is implemented (more ore less) with an Win32 Api call...


Solution

  • According to the documentation here: Encoding.GetEncoding Method (Int32), you can use Encoding.GetEncoding(0) to get the default encoding.