【发布时间】:2011-06-20 07:30:00
【问题描述】:
我来自中国,所以我的英语可能真的很差。我会尽力让你理解我的问题。 我想在我的 C# 项目中使用 PHP CLI。 我试过这样的代码
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();
}
由于返回的字符串不是我需要的,所以我使用 substring 来获得正确的结果,但有时我无法获得我真正想要的结果。 我想我的方法可能不正确,但是我在网上找不到任何资料。
【问题讨论】:
-
请格式化您的问题并将您的代码包装在代码标签中。
-
请说明实际结果和预期结果。
标签: c# php .net command-line-interface