0

I have a method in a .Net Core 6 Class Library that reads a CSV file that has 1252 encoding:

public void checkCsv(string pathCsvFile) {
    int codeEncoding = 1252;
    string line1 = "";
    foreach (var line in File.ReadAllLines(pathCsvFile, System.Text.Encoding.GetEncoding(codeEncoding))) {
        line1 = line;
        break;
    }

    // some more code here...
}

For some reason, if I call this method from a Winforms app (.Net Core 6), it works flawlessly. But if I call it from a WPF app (.Net Core 6), it fails with an exception:

No data is available for encoding 1252. For information on defining a custom encoding, see the documentation for the Encoding.RegisterProvider method.

Any help?

patsy2k
  • 471
  • 2
  • 8

1 Answers1

0

Ok, as per Murphy's laws, I found the answer right after I posted the question...

I have just to add this line before trying to access the file:

System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);

What I still don't understand tho, is why this line does not seem to be needed when called from the winforms app...

patsy2k
  • 471
  • 2
  • 8
  • I don't know the _exact_ reason why, but considering that whe .NET was rebooted as .NET Core in 2016, the .NET platform is now geared to be cross-platform and to avoid awkward _legacy_ things that we'd all rather forget (things like DOS/Windows coodepages...). – Dai Jul 17 '23 at 13:16
  • *this line does not seem to be needed when called from the winforms app*: it's needed – Jimi Jul 17 '23 at 14:03