Reading unicode from console

前端 未结 2 1323
有刺的猬
有刺的猬 2020-12-17 15:31

I am trying to read unicode string from a console in C#, for the sake of example, lets uset his one:

c:\\SVN\\D³ebugger\\src\\виталик\\Program.cs

2条回答
  •  一个人的身影
    2020-12-17 16:10

    This seems to work fine when targetting .NET 4 client profile, but unfortunately not when targetting .NET 3.5 client profile. Ensure you change the console font to Lucida Console.
    As pointed out by @jcl, even though I have targetted .NET4, this is only because I have .NET 4.5 installed.

    class Program
    {
        private static void Main(string[] args)
        {
            Console.InputEncoding = Encoding.Unicode;
            Console.OutputEncoding = Encoding.Unicode;
    
            while (true)
            {
                string s = Console.ReadLine();
    
                if (!string.IsNullOrEmpty(s))
                {
                    Debug.WriteLine(s);
    
                    Console.WriteLine(s);
                }
            }
        }
    }
    

    enter image description here

提交回复
热议问题