Skip to content

Commit

Permalink
Ensure no IOException/SecurityException is thrown when setting consol…
Browse files Browse the repository at this point in the history
…e output encoding (#9833)
  • Loading branch information
GangWang01 authored Mar 7, 2024
1 parent 5089df8 commit 9af8ff2
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/Framework/EncodingUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -274,10 +274,17 @@ internal static Encoding BatchFileEncoding(string contents, string encodingSpeci
{
if (CurrentPlatformIsWindowsAndOfficiallySupportsUTF8Encoding())
{
// Setting both encodings causes a change in the CHCP, making it so we don't need to P-Invoke CHCP ourselves.
Console.OutputEncoding = Encoding.UTF8;
// If the InputEncoding is not set, the encoding will work in CMD but not in PowerShell, as the raw CHCP page won't be changed.
Console.InputEncoding = Encoding.UTF8;
try
{
// Setting both encodings causes a change in the CHCP, making it so we don't need to P-Invoke CHCP ourselves.
Console.OutputEncoding = Encoding.UTF8;
// If the InputEncoding is not set, the encoding will work in CMD but not in PowerShell, as the raw CHCP page won't be changed.
Console.InputEncoding = Encoding.UTF8;
}
catch (Exception ex) when (ex is IOException || ex is SecurityException)
{
// The encoding is unavailable. Do nothing.
}
return externalLanguageSetting;
}
else if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
Expand Down

0 comments on commit 9af8ff2

Please sign in to comment.