Encoding problem of Process.StandardInput or application executed from C# code

Using of StreamWriter created the next way (instead of StandardInput) gives desired result:

StreamWriter utf8Writer = new StreamWriter(proc.StandardInput.BaseStream, Encoding.UTF8);
utf8Writer.Write(...);
utf8Writer.Close();

I've just encountered this problem and was unable to use the Console.InputEncoding technique because it only seems to work in console applications.

Because of this I tried Victor's answer, however I encountered the same issue as the commenter MvanGeest where by the BOM was still being added. After a while I discovered that it is possible to create a new instance of UTF8Encoding that has the BOM disabled, doing this stops the BOM from being written. Here is a modified version of Victor's example showing the change.

StreamWriter utf8Writer = new StreamWriter(proc.StandardInput.BaseStream, new UTF8Encoding(false));
utf8Writer.Write(...);
utf8Writer.Close();

Hope this saves someone some time.


Another solution is to set the Console.InputEncoding before you create the process.

Console.InputEncoding = Encoding.UTF8;