C# 如何在C中使用PHPCLI
我来自中国,所以我的英语可能很差。我会尽力让你明白我的问题。 我想在我的C项目中使用PHPCLI。 我试过这样的代码C# 如何在C中使用PHPCLI,c#,php,.net,command-line-interface,C#,Php,.net,Command Line Interface,我来自中国,所以我的英语可能很差。我会尽力让你明白我的问题。 我想在我的C项目中使用PHPCLI。 我试过这样的代码 Process p = new Process(); p.StartInfo.FileName = "cmd.exe"; p.StartInfo.Arguments = command; p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardInput = true; p.StartInfo.Redire
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();
}
由于返回的字符串不是我需要的,所以我使用子字符串来获得正确的结果,但有时我无法得到我真正想要的结果。
我认为我的方法可能不正确,但我在互联网上找不到任何信息。如果我按你的代码示例看,这个问题没有意义。但是,根据您的问题,您可以从CLI执行PHP脚本,并使用如下方式收集输出:
System.Diagnostics.Process proc = new System.Diagnostics.Process();
string sOutput = "";
proc.EnableRaisingEvents = false;
proc.StartInfo.FileName = "php.exe";
proc.StartInfo.Arguments = "-f file.php";
proc.StartInfo.RedirectStandardOutput = true;
System.IO.StreamReader hOutput = proc.StandardOutput;
proc.WaitForExit(2000);
if(proc.HasExited)
sOutput = hOutput.ReadToEnd();
这段代码的一半是我自己的,其余部分是从我找到的via派生的
希望这就是你想要的 请格式化您的问题,并将代码包装在代码标签中。请解释实际结果和预期结果。是的,非常感谢您的帮助,我已使用与您类似的方法解决了此问题谢谢