Search code examples
c#windowsemoji.net-9.0

Emojis not displaying properly after publishing exe in .NET 9


After publishing a console app (using dotnet publish), the console app does not display the emojis properly in Windows Terminal.

Interestingly, if I just use dotnet run, the emojis appear properly in Windows Terminal.

I published the console app using --no-self-contained and without it. Both result with the emojis not showing properly.

This is happening on both Windows 10 version 22H2 and Windows 11 version 23H2.

I'm using .NET 9.0.200.

Sample code:

const string CHECKMARK1 = "✅";
const string CHECKMARK2 = "\u2705";

Console.WriteLine(CHECKMARK1);
Console.WriteLine(CHECKMARK2);
Console.WriteLine("✅");
Console.WriteLine("\u2705");

Output using dotnet run in Windows Terminal:

✅
✅
✅
✅

EXE output after using dotnet publish in Windows Terminal:

?
?
?
?

Also, for thoroughness, here are the commands I used for publishing:

dotnet.exe publish "c:\path\to\emoji.csproj" --runtime "win-x64" --no-self-contained /p:PublishSingleFile=true

dotnet.exe publish "c:\path\to\emoji.csproj" --runtime "win-x64" /p:PublishSingleFile=true

I would appreciate any insight into why this is happening.


Solution

  • Before the first System.Console output call, you have to execute:

    System.Console.OutputEncoding = System.Text.Encoding.UTF8;
    

    In contrast to some other Console calls, this property assignment can throw an exception if the entry assembly has a wrong OutputType. It should be <OutputType>Exe</OutputType>.