Set C# console application to Unicode output

后端 未结 2 1204
不知归路
不知归路 2020-12-10 14:16

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 intern

2条回答
  •  执念已碎
    2020-12-10 14:38

    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 Main(string[] args)
        {
            SetConsoleOutputCP(65001);
            SetConsoleCP(65001);
    
            Console.WriteLine("This is how you say hello in Japanese: こんにちは");
    
            return 0;
        }
    }
    

    Output:

提交回复
热议问题