Started Process from .NET but RedirectedStandardOu

2019-07-27 15:24发布

I am trying to call php's HTML purifier from .NET using this code:

    Process myProcess = new Process();

    myProcess.StartInfo.FileName = "C:\Path\to\php.exe";
    myProcess.StartInfo.Arguments = "C:\Path\to\purify.php";
    myProcess.StartInfo.UseShellExecute = false;
    myProcess.StartInfo.RedirectStandardOutput = true;
    myProcess.StartInfo.RedirectStandardInput = true;

    myProcess.Start();

    StreamWriter myStreamWriter = myProcess.StandardInput;

    String inputText;

    inputText = txtCodes.Text;
    if (inputText.Length > 0)
    {
        myStreamWriter.Write(inputText);
    }
   myStreamWriter.Close();

    labMsg.Text = myProcess.StandardOutput.ReadToEnd();

    myProcess.WaitForExit();

    myProcess.Close();

.. and all works fine except ... I am not able to get back non-asci characters. For example providing some Korean characters in the input returns questionmarks as output.

This happens even if the HTMLPurifier function is bypased and I am just trying to simple provide the input .NET, store it in php variable, and echo that variable back to output.

Any ideas?

2条回答
何必那么认真
2楼-- · 2019-07-27 15:41

Try

myProcess.StartInfo.StandardOutputEncoding = System.Text.Encoding.UTF8;
查看更多
做个烂人
3楼-- · 2019-07-27 15:48

Thanks for the pointer. I did actually managed to solve it. The catch was to explicitly specify UTF-8 for BOTH input and Output. In the end the woking code looks like this:

Process myProcess = new Process();

    myProcess.StartInfo.FileName = "C:\Path\to\php.exe";
    myProcess.StartInfo.Arguments = "C:\Path\to\purify.php";

    myProcess.StartInfo.UseShellExecute = false;
    myProcess.StartInfo.RedirectStandardOutput = true;
    myProcess.StartInfo.RedirectStandardInput = true;
    myProcess.StartInfo.StandardOutputEncoding = Encoding.UTF8;

    myProcess.Start();
    StreamWriter myStreamWriter = new StreamWriter(myProcess.StandardInput.BaseStream, Encoding.UTF8);

    String inputText;
    inputText = txtCodes.Text;

    if (inputText.Length > 0)
    {
        myStreamWriter.Write(inputText);
    }

    myStreamWriter.Close();

    labMsg.Text = myProcess.StandardOutput.ReadToEnd();


    myProcess.WaitForExit();

    myProcess.Close();
查看更多
登录 后发表回答