Problems typing non-English characters into a C # console application

I am trying to create a C # console application with Visual Studio 2010 in English Windows 7 Ultimate 64-bit. When I try to copy a path with non-ASCII characters and then paste it into the console application, non-ASCII characters will turn into ???. Is there any way to fix this?

Here is what I copy: C:\Test Folder\

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

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

But even if I change the font, it C:\Test Folder\still becomes C:\Test Folder\?????????in the variable strLineUserInputwhen I test it with the debugger.

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

So, if I do this, then:

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

My strLineUserInputbecomes nullif I read the text above.

+5
4

:

  • Lucida Console , .
  • :

    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);
    }
    

:

C:\Test Folder\
C:\Test Folder\
C:\Test Folder\

[]

, ReadKey, ( Lucida Console):

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();
}
+4

#, :

using System.Runtime.InteropServices;

[DllImport("kernel32.dll", SetLastError = true)]
static extern IntPtr GetStdHandle(int nStdHandle);

[DllImport("kernel32.dll")]
static extern bool ReadConsoleW(IntPtr hConsoleInput, [Out] byte[]
    lpBuffer, uint nNumberOfCharsToRead, out uint lpNumberOfCharsRead,
    IntPtr lpReserved);

public static IntPtr GetWin32InputHandle()
{
    const int STD_INPUT_HANDLE = -10;
    IntPtr inHandle = GetStdHandle(STD_INPUT_HANDLE);
    return inHandle;
}

public static string ReadInputLineAsUTF8()
{
    //I can't seem to find a way not to hardcode the size here???
    const int bufferSize = 1024 * 2;
    byte[] buffer = new byte[bufferSize];

    uint charsRead = 0;
    ReadConsoleW(GetWin32InputHandle(), buffer, bufferSize, out charsRead, (IntPtr)0);

    //Make new array of data read
    byte[] buffer2 = new byte[charsRead * 2];
    for (int i = 0; i < charsRead * 2; i++)
    {
        buffer2[i] = buffer[i];
    }

    //Convert string to UTF-8
    return Encoding.UTF8.GetString(Encoding.Convert(Encoding.Unicode, Encoding.UTF8, buffer2)).Trim();
}
0

, .

.

, , .

Unicode , , . , "?".

> :

  • Open control panel
  • Choose region and language
  • Overview Current language for non-Unicode
  • If it is not installed in Russian, try "Change the system language" and install in Russian.
0
source

It is very simple if you just want the input copy to be pasted into your text box. You have a control box in your application, and you can have code to change the font like this.

enter code here
 private void checkBox1_CheckedChanged(object sender, EventArgs e)
    {
        if (checkBox1.Checked)
        {
            textBox1.Font = new Font("Your font", 10);
        }
        else 
        {
            textBox1.Font = new Font("Times New Roman", 10);
        }
    }

Or, if you always insert non-English, you can change the font property in the text box.

-1
source

All Articles