简体   繁体   中英

How can I Input or output Korean in right way in a winform program

        Console.InputEncoding = Encoding.Unicode;
        Console.OutputEncoding = Encoding.GetEncoding("utf-8");
        Console.WriteLine("开始测试starttest전소미123");
        string input = Console.ReadLine();
        Console.WriteLine(input);
        Console.ReadKey();
        Console.ReadKey();

when I run these codes, I get exactly the same string, as below

在此处输入图片说明

And I can't get the right characters if I don't set InputEncoding or OutputEncoding or both.

But I can't set Console.OutputEncoding in a winform program. It threw An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll. This error happened on the second line below,

Console.OutputEncoding = Encoding.Unicode;

code

        Console.InputEncoding = Encoding.Unicode;
        Console.OutputEncoding = Encoding.Unicode;
        Process process = new Process()
        {
            StartInfo = new ProcessStartInfo
            {
                FileName = $".\\test.exe",
                Arguments = "",
                CreateNoWindow = true,
                UseShellExecute = false,
                ErrorDialog = true,
                //AutoFlush = true,
                //RedirectStandardInput = true,
                RedirectStandardOutput = true,
                RedirectStandardError = true,
                StandardOutputEncoding = Encoding.GetEncoding("utf-8"),
            },
        };
        process.Start();
        string text = process.StandardOutput.ReadLine();

I had searched similar questions online and tested many times for hours, just can't figure it out.

What text editor and encoding are you using for the exe file? I think for example Notepad's default encoder is ANSI which I know do not support Asian symbols. If you want to change the encoder for Notepad click File -> Save as, then you can change the encoding just left to the Save button and change the encoding to UTF-8 or Unicode.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM