How to specify stdin encoding when starting a process with Process.Start in .NET
// // When using the .NET System.Diagnostics.Process object to start a process, there is no provision // for providing the character encoding for standard input. This is a problem for example when // spawning a console application (such as perl.exe in my case) that expects 8-bit characters // encoded with a particular codepage (874, for Thai, in my case). // // Prior to .NET 2.0, there was also no provision for specifying the character encoding for // standard output. This was rectified with the addition of ProcessStartInfo.StandardOutputEncoding, // but mysteriously enough, standard input was left out of this party. // // Here is a workaround (requires .NET 2.0 or greater; if you must run on earlier version of .NET, // similar handling of the BaseStream for StandardOutput is possible): // using System.Diagnostics; using System.IO; //... ProcessStartInfo psi = new ProcessStartInfo(); psi.FileName = @"c:\foo\consoleapp.exe"; psi.UseShellExecute = false; psi.RedirectStandardOutput = true; psi.StandardOutputEncoding = Encoding.GetEncoding(874); // .NET 2.0 or greater psi.RedirectStandardInput = true; Process p = new Process(); p.StartInfo = psi; p.Start(); string stdin = "TIS-620 Thai text going in: \x0e01\x0e01\x0e01\x0e01\n"; byte[] rg_b = Encoding.GetEncoding(874).GetBytes(stdin); Stream bs = p.StandardInput.BaseStream; bs.Write(rg_b,0,rg_b.Length); bs.Close(); string stdout_out = p.StandardOutput.ReadToEnd();
