我希望在我的 C# 控制台应用程序中嵌入一个 exe 文件(特别是“python.exe”,Python 2.7 的解释器),这意味着我希望用户输入命令,将解释器的输出接收到字符串变量中并使用 Console.WriteLine 来打印该输出。到目前为止,我的代码是:
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);
}
}
我可以编写“print 5”并获得正确的输出,但是再次编写时出现以下错误:
无法写入已关闭的 TextWriter。
我相信这个错误是因为我关闭了 StandardInput,但是没有那行代码我根本没有得到任何输出。我可以使用什么来向 exe 发送多个命令?
先感谢您!