25

I need to create System.Encoding for 1251 codepage.

On my russian Windows I use

Encoding encoding = Encoding.Default

I am afraid this will produce different results depending on Windows

Captain Comic
  • 14,954
  • 43
  • 105
  • 144

3 Answers3

44

Correct, you will get different results on different machines if you use Encoding.Default.

If you want a specific codepage, you can use Encoding.GetEncoding:

Encoding encoding = Encoding.GetEncoding("windows-1251");
kiewic
  • 15,130
  • 13
  • 74
  • 95
Michael Madsen
  • 53,013
  • 7
  • 71
  • 82
  • 2
    Yeap, I should have just RTFM. Another option is specifying codepage by name instead of number as you did. In my case that would be windows-1251 – Captain Comic Oct 19 '10 at 11:22
12

For .NET Core you also need to reference the System.Text.Encoding.CodePages package and then use Encoding.RegisterProvider:

Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
razon
  • 3,454
  • 2
  • 28
  • 43
2

The .NET Framework/.NET Core supports a large number of character encodings and code pages. To retrieve an encoding that is present in the .NET Framework/.NET Core pass the EncodingProvider object to the Encoding.RegisterProvider method to make the encodings supplied by the EncodingProvider object available to the common language runtime. Microsoft Document Reference

System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
Charan Ghate
  • 1,314
  • 14
  • 32