使用C#作为Python的输入和输出

使用C#作为Python的输入和输出,c#,python,C#,Python,我正在尝试使用C#读写Python脚本。我可以成功地写入脚本并让它在我的C#命令上运行函数。但我不知道如何将RedirectStandardOutput与StreamReader一起使用来接收python代码的结果(返回值或写入屏幕语句);它只是锁定了程序 C#代码: Python代码: import sys def pyAdd(x,y): z = x + y # Print to console (if desired) print "Using pyAdd, th

我正在尝试使用C#读写Python脚本。我可以成功地写入脚本并让它在我的C#命令上运行函数。但我不知道如何将RedirectStandardOutput与StreamReader一起使用来接收python代码的结果(返回值或写入屏幕语句);它只是锁定了程序

C#代码:

Python代码:

import sys

def pyAdd(x,y):
   z = x + y 
   # Print to console (if desired)   
   print "Using pyAdd, the sum of %f and %f is %f" % (x, y, z)
   # Return a value back to the C# program
   return z

def pyMultiply(x,y):
   z = x * y 
   # Print to console (if desired)   
   print "Using pyMultiply, the product of %f and %f is %f" % (x, y, z)
   # Return a value back to the C# program
   return z

while True:  
   testVar = raw_input("Enter the function name: ")
   splitInput = testVar.split(",")

   if splitInput[0] == "pyAdd":  
      pyAdd(float(splitInput[1]), float(splitInput[2]))  
   elif splitInput[0] == "pyMultiply":
      pyMultiply(float(splitInput[1]), float(splitInput[2]))  
   else:
      print "Function %s not available" % splitInput[0]

使用
thisInfo.RedirectStandardOutput=true

string output = thisInfo.StandardOutput.ReadToEnd();
thisInfo.WaitForExit();
Console.WriteLine("Output:");
Console.WriteLine(output);

带有
thisInfo.RedirectStandardOutput=true

string output = thisInfo.StandardOutput.ReadToEnd();
thisInfo.WaitForExit();
Console.WriteLine("Output:");
Console.WriteLine(output);

这是一个很久以前的问题,但你最终解决了这个问题吗?我还试图通过C#代码运行Python.exe。但是,导入需要很长时间,因为Python.exe每次都会执行。我还想在加载文件后使用不同的参数调用Python函数,以避免每次导入字符。这是一个很久以前的问题,但您最终解决了这个问题吗?我还试图通过C#代码运行Python.exe。但是,导入需要很长时间,因为Python.exe每次都会执行。我还希望在加载文件后使用不同的参数调用Python函数,以避免每次导入字符。