我希望在我的C#控制台应用程序中嵌入一个exe文件(特别是Python 2.7的解释器中的“python.exe”),这意味着我希望用户输入命令,将解释器的输出接收到字符串变量并使用Console。 WriteLine打印输出。到目前为止我的代码是:在C#控制台中嵌入一个exe文件?
ProcessStartInfo processStartInfo = new ProcessStartInfo("C:/Python27/python.exe");
processStartInfo.RedirectStandardInput = true;
processStartInfo.RedirectStandardOutput = true;
processStartInfo.UseShellExecute = false;
Process process = Process.Start(processStartInfo);
if (process != null)
{
while (true)
{
process.StandardInput.WriteLine(Console.ReadLine());
process.StandardInput.Close();
string output = process.StandardOutput.ReadToEnd();
Console.WriteLine(output);
}
}
我可以写“打印5”,并得到正确的输出,但再次书写时,我收到以下错误:
Cannot write to a closed TextWriter.
我认为这个错误是因为我关闭了StandardInput,但没有这行代码,我根本没有得到任何输出。我可以使用什么发送多个命令到exe文件?
预先感谢您!
你可以调用process.StandardInput.Flush()而不是Close()吗? – scottm 2012-07-23 20:13:25
你有没有考虑过使用IronPython? – 2012-07-23 20:13:35
调用这个“嵌入”非常非正统。 :) – bzlm 2012-07-23 20:42:15