C#:问题运行shell命令 [英] C#: Problem running shell commands
本文介绍了C#:问题运行shell命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图让PHP解析器来运行一个页面,然后把结果返回到我的服务器,但是当我通过我的代码运行的命令,它没有返回值。我知道命令是正确的,因为如果我相同的路径手动运行它,它工作正常。继承人我的代码:
I'm trying to get the PHP parser to run a page and then return the results to my server, however when I run the command via my code, it returns nothing. I know the command is correct because if I run it manually with the same path, it works fine. Heres my code:
var p = new Process
{
StartInfo = new ProcessStartInfo("C:\\xampp\\php\\php.exe", path)
{
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true
}
};
var output = new StringWriter();
var error = new StringWriter();
p.OutputDataReceived += (sender, args) => output.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();
Console.WriteLine(res);
编辑:
有了这个当前的代码,它抛出代码中的异常,并没有输出
With this current code, it throws the exception in the code with no output.
推荐答案
设置工作目录路径
var p = new Process
{
StartInfo = new ProcessStartInfo("php", path)
{
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true,
WorkingDirectory = workingDir
}
};
这篇关于C#:问题运行shell命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文