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.
Follow these steps:
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();
}
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}");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With