-1

I want to give the user the possibility to execute some little code from my winforms application, written in c#. I'd like to use php, so I have found Phalanger.

I put two richTextEdit in my Form and I want to write php in richTextEdit1, press a button, and view the result in the richTextEdit2.

The php code is:

echo "Hello word!";

the button code is:

ScriptContext context = ScriptContext.CurrentContext;

MemoryStream ms;
StreamWriter sw;

ms = new MemoryStream();
sw = new StreamWriter(ms);

context.Output = sw ;
//context.Output = Console.Out;

var res = DynamicCode.Eval(
            richTextBox1.Text,
            false,/*phalanger internal stuff*/
            context,
            null,/*local variables*/
            null,/*reference to "$this"*/
            null,/*current class context*/
            "Default.aspx.cs",/*file name, used for debug and cache key*/
            1, 1,/*position in the file used for debug and cache key*/
            -1,/*something internal*/
            null/*current namespace, used in CLR mode*/
        );

richTextBox2.Text = Encoding.UTF8.GetString(ms.ToArray());

If I use Console.Out as output, it works fine, but is not usefull. If not, I have no result.

Can anyone help me?

Redax
  • 9,231
  • 6
  • 31
  • 39

1 Answers1

2

Simply call sw.Flush() before reading ms's content. I would recommend to wrap usage of sw into using statement before you read ms and specify the encoding explicitly using (sw = new StreamWriter(ms, Encoding.UTF8))

Jakub Míšek
  • 1,036
  • 9
  • 12