24

I have an issue with encoding of Process.StandartInput encoding. I am using some process in my Windows Form application but input should be UTF-8. Process.StandardInput.Encoding is read only so I can't set it to UTF-8 and it gets Windows default encoding which deteriorate native characters which are good in UTF-8. Two processes are used in the program: one writes output to a file and other reads. Since I can set up output encoding to UTF-8 that part is working properly but reading back is the part where I am having problems. I'll include the part where I use the process.

ProcessStartInfo info = new ProcessStartInfo("mysql");
info.RedirectStandardInput = true;
info.RedirectStandardOutput = false;
info.Arguments = mysqldumpstring;
info.UseShellExecute = false;
info.CreateNoWindow = true;
Process p1 = new Process();
p1.StartInfo = info;
p1.Start();
string res = file.ReadToEnd();
file.Close();

// where encoding should be Encoding.UTF8;
MessageBox.Show(p1.StandardInput.Encoding.EncodingName); 

p1.StandardInput.WriteLine(res);
p1.Close(); 

6 Answers 6

27

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();
2
  • 1
    Thanks. Solves the problem nicely. Note that it also works for StandardOutput. Aug 20, 2015 at 4:17
  • Unfortunately, this no longer seems to work - a UTF16 (!) BOM (FF FE) is still written before the data. Nick's workaround seems to be the only, ugly, solution for now.
    – MvanGeest
    May 26, 2016 at 23:00
13

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.

10

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

 Console.InputEncoding = new UTF8Encoding(false);
1
  • What worked for me was Console.InputEncoding = new System.Text.UTF8Encoding(false); to ensure UTF BOM is not written. Also had to put this before referencing Process.StandardInput Mar 10, 2022 at 3:07
2

got it working now set my application output type to console application and managed to hide the console window appears before the forms. It basically works like normal only when program run, a console windows pops and hides.

static class Program
{
    [DllImport("kernel32.dll")]
    static extern bool AttachConsole(int dwProcessId);
    private const int ATTACH_PARENT_PROCESS = -1;

    [DllImport("kernel32.dll", SetLastError = true)]
    public static extern bool SetConsoleCP(
         uint wCodePageID
         );

    [DllImport("kernel32.dll", SetLastError = true)]
    public static extern uint GetConsoleCP();

    [DllImport("user32.dll")]
    public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

    [DllImport("user32.dll")]
    static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Console.Title = "Stok";
        // redirect console output to parent process;
        // must be before any calls to Console.WriteLine()
        AttachConsole(ATTACH_PARENT_PROCESS);
        System.Console.InputEncoding = Encoding.UTF8;

        IntPtr hWnd = FindWindow(null, "Stok"); //put your console window caption here

        if (hWnd != IntPtr.Zero)
        {
            //Hide the window
            ShowWindow(hWnd, 0); // 0 = SW_HIDE
        }

        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }
}
2

I had various encoding problems when sending StandardInput to "cmd.exe". I was generating a batch file that used mklink with various non ASCII filenames.

I came to the conclusion that wrapping StandardInput.BaseStream in a UTF8 StreamWriter does not work properly (at least with cmd.exe and the filenames I had). Changing encoding fixed some but then broke others.

This is what worked for me:

  • Save batch file to a temporary file as UTF8 no BOM, like so: File.WriteAllText("temp.bat", batchText, new UTF8Encoding(false));
  • Set StandardOutputEncoding and StandardErrorEncoding to Encoding.UTF8
  • Sent to StandardInput the 2 lines: "chcp 65001" + Environment.NewLine + "temp.bat" + Environment.NewLine

This is only good for "cmd.exe" the command "chcp 65001" tells it to use UTF8.

Forget about StandardInput encoding the 2 lines being sent are ASCII

0

Actually there is a lot better way to do so nowadays. Instead of wrapping into extra writer, you can configure Encoding for original one from the very beginning:

Process.Start(new ProcessStartInfo("mysql")
{
    UseShellExecute = false,
    RedirectStandardInput = true,
    StandardInputEncoding = new UTF8Encoding(false),
});

Also, note that Encoding.UTF8 might emit BOM, so, similarly to default StreamWriter constructor, it is always a good idea to create new UTF8Encoding(false) to disable BOM (and you can cache it in static field, similarly to what MS are doing internally)

Also Nick Randell's answer is underestimated, it works indeed (as long as you are fine to have it globally for your process as well). But in "Big Enterprise" this is not very good solution, different teams might get into conflict on what should be by default.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.