dotnet / dotnet/winforms

Fix Encoding.Default regressions

Open
#3,539 3 comments 1 reaction 0 assignees View on GitHub
area-Interop help wanted
Dominant language
C#
Stars
4.9k
Forks
1.1k
Avg merge
1d 13m
Merged PRs (30d)
85

Description

**.NET Core Version:**
any .NET Core version

**Have you experienced this same bug with .NET Framework?:**
no, regression against Desktop Framework

**Problem description:**

.NET Core changed the behavior of `Encoding.Default` to return UTF8 encoding, while Desktop (from which WinForms is derived) returned the Windows ANSI encoding (which is also used for interop). Any usage of `Encoding.Default` in WinForms is now very likely to be incorrect and must be reviewed and updated.

See issue #3032 and PR #3100, #3141, #3210 for the previously done corrections, but [more remain](https://github.com/dotnet/winforms/search?q=%22Encoding.Default%22&unscoped_q=%22Encoding.Default%22) to be fixed.

The correct pattern for porting Desktop Framework usage of `Encoding.Default` is now to use `CodePagesEncodingProvider.Instance.GetEncoding(0) ?? Encoding.UTF8` instead, which retrieves the ANSI encoding Windows uses for interop. (The codepage based encoding is null if Windows was configured to be using UTF8 instead of a normal codepage, see discussion in #3032.)

For call sites which aren't related to interop there needs to be discussion and case-by-case decision of whether to replace `Encoding.Default` or whether to use UTF8 going forward. They could still be updated to refer to the UTF8 encoding explicitly for clarity.

**Expected behavior:**
WinForms is updated to respect the changed behavior of `Encoding.Default`

**Minimal repro:**
n/a (issue raised due to manual inspection of source code, analysis of scenarios should happen as part of the issue, if required)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.