I'm from China, so my english maybe is really poor. I will try my best to make you understand my question. I want to use PHP CLI in my C# project. I tried code like this
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = command;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
try
{
p.Start();
p.StandardInput.WriteLine(command);
p.StandardInput.WriteLine("exit");
p.WaitForExit(1000);
StreamReader reader = new StreamReader(p.StandardOutput.BaseStream, Encoding.GetEncoding("utf-8"));
string text= reader.ReadToEnd();
if (text.IndexOf(command) != -1)
{
int start = text.IndexOf(command) + command.Length;
string endstring = rootpath + ">exit";
int end = text.IndexOf(endstring);
text = text.Substring(start, text.Length - start - endstring.Length).Trim();
return text;
}
return "";
}
catch (System.Exception ex)
{
return "";
}
finally
{
p.Close();
}
As the returned string is not what I need, I use substring to get the correct results, but sometimes I can't get what I really want. I think my method might not be correct,but I can't find any information on the Internet.