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

1

6

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

Kelcey answered 10/3, 2016 at 23:27 Comment(0)
W
8

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

Whose answered 11/7, 2016 at 15:6 Comment(1)
And it works at least under .NET 4.6 and universal windows apps. I couldn't test it with a .NET Core console operation - didn't have enough patience with Visual Studio..Kelcey

© 2022 - 2024 — McMap. All rights reserved.