14

I have a C# console application, and I was trying to do some ASCII art within it. However, some of the characters I wanted to use are Unicode. So, I was searching the internet/SO and couldn't find a consolidated answer on how to set the console to be Unicode in a C# console application.

TDLR: How do I set the console in a C# console application to be Unicode?

Edit: I did find this post after searching for something not related to this question.

Community
  • 1
  • 1
Michael Yanni
  • 1,476
  • 4
  • 17
  • 29

3 Answers3

24

It turns out that there are multiple things you need to set up in order to make the console display Unicode characters.

  1. Set the console to a Unicode supported font. To do this, run your C# console application once with Console.ReadKey(); so the window stays open. Right click on the title bar of the window and select Properties. These options will persist when debugging through Visual Studio. You might need to use the Default menu instead for persisting the options throughout the system. In the Fonts tab, you need to set the font to Lucida Console. This font supports Unicode characters. The related post can be found here.
  2. Set the console's code page to UTF-8. This one is a bit tricky. Because, you have to execute a command in the console window to change the code page. For whatever reason, this option is not available as a console preference. To do this, you'll need to make a separate cmd.exe process, and use this instead of the normal console provided.

    var cmd = new Process
    {
        StartInfo =
        {
            FileName = "cmd.exe",
            RedirectStandardInput = true,
            RedirectStandardOutput = true,
            CreateNoWindow = true,
            UseShellExecute = false
        }
    };
    cmd.Start();
    
    cmd.StandardInput.WriteLine("chcp 65001");
    cmd.StandardInput.Flush();
    cmd.StandardInput.Close();
    

    The first part of the code above will create a new cmd.exe process. The settings given to StartInfo will make sure that Console is redirected to this new process. The second part of the code sends a command to this console window and runs it. That command, chcp 65001, sets the console's code page to UTF-8. Related posts can be found here and here.

  3. Set the OutputEncoding to UTF-8. This is the only way that Console.WriteLine will actually output Unicode characters. Setting this is very simple.

    Console.OutputEncoding = Encoding.UTF8;
    

    Now, any output from Console will be in Unicode. The related post can be found here.

So, that's it! I hope this information helps someone. :-)

Community
  • 1
  • 1
Michael Yanni
  • 1,476
  • 4
  • 17
  • 29
  • 1
    If you set both `Console.InputEncoding` and `Console.OutputEncoding` to `Encoding.UTF8`, the code page is changed internally, so no need to start process `chcp`. – Gerardo Grignoli Mar 20 '22 at 07:39
13

Another option is to use P/Invoke to change the code page directly:

class Program
{
    [DllImport("kernel32.dll", SetLastError = true)]
    private static extern bool SetConsoleOutputCP(uint wCodePageID);

    [DllImport("kernel32.dll", SetLastError = true)]
    private static extern bool SetConsoleCP(uint wCodePageID);

    static async Task<int> Main(string[] args)
    {
        SetConsoleOutputCP(65001);
        SetConsoleCP(65001);

        Console.WriteLine("This is how you say hello in Japanese: こんにちは");

        return 0;
    }
}

Output:

Japanese Characters in the console

Cillié Malan
  • 864
  • 11
  • 13
  • Does not work on .net framework. Might bis be a thing in the .net core console? – boop Feb 23 '21 at 09:58
  • The code above is .Net Framework. Note that non-ascii characters (like in the screenshot) only show up if the console font supports it. The font set in the console above is NSimSun. – Cillié Malan Feb 24 '21 at 10:07
  • This is the solution that worked for me when trying to get Arabic letters in the console output (.NET 5 on windows). Although I changed the value from 65001 to 1256 (the Arabic encoding code page value). – Julie Lerman Oct 10 '21 at 13:36
  • also want to note that console was at default font. This code caused the console font to change to courier new which supported the Arabic chars. – Julie Lerman Oct 11 '21 at 13:28
  • I prefer this solution over the accepted answer because it's clean and works out of the box for me (it's essentially the equivalent of running "chcp 65001" in cmd.exe before launching my C# app). – David Airapetyan Oct 23 '21 at 22:29
  • If you do this, do it before calling any method in the System.Console class. Otherwise it will initialize to other encoding. – Gerardo Grignoli Mar 20 '22 at 07:37
0

The solution I prefer is to change the active console code page of my profile to utf-8 using region under control panel. (see picture) Restart and select a font which can display the necessary characters.

Select this tickbox