i want to get the result of a php file with Process.Start, my code:
public string RunPHP(string filename)
{
var p = new Process
{
StartInfo = new ProcessStartInfo("php", "-f \"" + filename + "\"")
{
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true
}
};
Console.WriteLine("'" + p.StartInfo.Arguments + "'");
var output = new StringWriter();
var error = new StringWriter();
p.OutputDataReceived += (sender, args) => { output.WriteLine(args.Data); Console.WriteLine(args.Data); };
p.ErrorDataReceived += (sender, args) => error.WriteLine(args.Data);
p.Start();
p.BeginOutputReadLine();
p.BeginErrorReadLine();
p.WaitForExit();
if (p.ExitCode != 0)
{
throw new Exception(string.Format(
"PHP failed with the following output:{0}{1}",
/* {0} */ Environment.NewLine,
/* {1} */ error.GetStringBuilder().ToString()));
}
var res = output.GetStringBuilder().ToString();
return res;
}
but i get the result only, if i'm press enter in the console application window. (php 7)
how can i avoid this?