The only effect that StandardOutputEncoding has no effect on the standard execution of the executable application. The only thing he does is set the StreamReader encoding, which is on top of the stdout binary stream captured from the application being launched.
This is normal for applications that initially display UTF8 or Unicode stdout, but most Microsoft utilities do not, and instead encode the results only for the console code page. The console code page is manually set using the WIN32 SetConsoleOutputCP and SetConsoleCP , and it must be forcibly redirected to UTF8 if that is what you would like to read. This must be done on the console, in which exe is executed internally, and, as far as I know, cannot be executed from the .NET host environment.
Thus, I wrote a proxy application called UtfRedirect, the source code of which I published on GitHub in accordance with the terms of the MIT license, which is designed to be created in the .NET host, and then told which exe to execute. It will install the code page for the console of the target subordinate exe, then run it and pass stdout back to the host.
An example of calling UtfRedirector:
//At the time of creating the process: _process = new Process { StartInfo = { FileName = application, Arguments = arguments, RedirectStandardInput = true, RedirectStandardOutput = true, StandardOutputEncoding = Encoding.UTF8, StandardErrorEncoding = Encoding.UTF8, UseShellExecute = false, }, }; _process.StartInfo.Arguments = ""; _process.StartInfo.FileName = "UtfRedirect.exe" //At the time of running the process _process.Start(); //Write the name of the final slave exe to the stdin of UtfRedirector in UTF8 var bytes = Encoding.UTF8.GetBytes(application); _process.StandardInput.BaseStream.Write(bytes, 0, bytes.Length); _process.StandardInput.WriteLine(); //Write the arguments to be sent to the final slave exe to the stdin of UtfRedirector in UTF8 bytes = Encoding.UTF8.GetBytes(arguments); _process.StandardInput.BaseStream.Write(bytes, 0, bytes.Length); _process.StandardInput.WriteLine(); //Read the output that has been proxied with a forced codepage of UTF8 string utf8Output = _process.StandardOutput.ReadToEnd();
source share