Wrong encoding in DataReceivedEventArgs

Posted by user2102508 on Programmers See other posts from Programmers or by user2102508
Published on 2014-05-26T08:10:42Z Indexed on 2014/05/26 22:00 UTC
Read the original article Hit count: 672

Filed under:
|

I start cmd.exe process and redirect stdin to pass script to it and redirect stdout and stderr to read cmd's output. Here is the code of my DataReceivedEventHandler:

(o, a) => {
    if(!String.IsNullOrEmpty(a.Data)) {
        bw.Write(a.Data.ToUTF8());
        bw.Write((byte)'\n');
    }
}

In the code bw is instance of BinaryWriter, ToUTF8 is string extension method, that converts a string to UTF8 encoded byte array. When I use this code in a separate process it works well, however when I use this code as a shared library inside some other process a.Data doesn't contain valid localized characters (like russian characters for example).

So how should I convert characters? How to get cmd's OEM encoding? Why does the code works well in a separate process and doesn't work as a shared library inside some other process?

© Programmers or respective owner

Related posts about c#

Related posts about .NET