Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems with the input of non-English characters into a C# console app

I'm trying to build a console C# application with Visual Studio 2010 on the English Windows 7 Ultimate 64-bit. When I try to copy a path with non-ASCII characters and then paste it into my console app the non-ASCII characters turn into ???. Is there any way to fix this?

Here's what I'm copying: C:\Test Folder\документи

And this is the code (after a suggested link above):

Console.OutputEncoding = System.Text.Encoding.UTF8;
string strLineUserInput = Console.ReadLine();

But even if I change the font, the C:\Test Folder\документи still becomes C:\Test Folder\????????? in strLineUserInput variable when I test it with a debugger.

Also note that unlike the link "duplicate post", I need these characters on the input.

So if I do this then:

Console.InputEncoding = System.Text.Encoding.UTF8;
string strLineUserInput = Console.ReadLine();

My strLineUserInput becomes null if I read the text above.

like image 604
c00000fd Avatar asked Feb 19 '13 23:02

c00000fd


2 Answers

Follow these steps:

  1. Change the console window font to Lucida Console for both when debugging / not debugging.
  2. Execute the following code:

    public static void Main(String[] args)
    {
        Console.OutputEncoding = System.Text.Encoding.GetEncoding("Cyrillic");
        Console.InputEncoding = System.Text.Encoding.GetEncoding("Cyrillic");
    
        Console.WriteLine(@"C:\Test Folder\документи");
        // input C:\Test Folder\документи
        string strLineUserInput = Console.ReadLine();
        Console.WriteLine(strLineUserInput);
    }
    

The output should be:

C:\Test Folder\документи
C:\Test Folder\документи
C:\Test Folder\документи

[UPDATE]

Maybe you would like to use the ReadKey method in order to have it working (you still have to use the Lucida Console font):

static void Main(string[] args)
{
    Console.OutputEncoding = Encoding.UTF8;
    Console.InputEncoding = Encoding.UTF8;

    string s = @"C:\Test Folder\документи";
    Console.WriteLine(s);

    // input C:\Test Folder\документи
    var strInput = ReadLineUTF();

    Console.WriteLine(strInput);
}

static string ReadLineUTF()
{
    ConsoleKeyInfo currentKey;

    var sBuilder = new StringBuilder();
    do
    {
        currentKey = Console.ReadKey();
        // avoid capturing newline
        if (currentKey.Key != ConsoleKey.Enter)
            sBuilder.Append(currentKey.KeyChar);

    }
    // check if Enter was pressed
    while (currentKey.Key != ConsoleKey.Enter);

    // move on the next line
    Console.WriteLine();

    return sBuilder.ToString();
}
like image 133
Alex Filipovici Avatar answered Oct 05 '22 13:10

Alex Filipovici


the code below helps me. Please use Encoding.Unicode instead of Encoding.UTF8

  Console.OutputEncoding = Console.InputEncoding = Encoding.Unicode;
  Console.Write("Введите свое имя: ");
  string name = Console.ReadLine(); 
  Console.WriteLine($"Привет {name}"); 
like image 21
Larissa Savchekoo Avatar answered Oct 05 '22 13:10

Larissa Savchekoo