-1

How can i change the default encoding for the Net Core console application?

I'm using a legacy library written in .Net framework that needs to read files in windows-1251 code page but won't allow me to specify it as a parameter. Only option available is read from file.

The only way i see to make it work, without changing the file codepage, is to change default code page during that operation.

I enabled codepage provider using (From package System.Text.Encoding.CodePages):

Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

Any clues?

MiguelSlv
  • 14,067
  • 15
  • 102
  • 169

1 Answers1

0

Found an workaround that maybe useful for someone else.

In my case the library doesn't force the code page, so by converting the file to UTF8 and writing the BOM resolved the issue. The library now can properly read any character in the file.

This solution can add significant IO so use with care.

var content = File.ReadAllText(Filename, Encoding.GetEncoding("windows-1252"));
File.WriteAllText(Filename, content, new UTF8Encoding(true)); //write back in UTF8  with BOM
... open 'filename' with the library
MiguelSlv
  • 14,067
  • 15
  • 102
  • 169