一种工作方法,但可能不是最干净和最安全的,是调用Cython或Cython的Python客户端,因为在NuGET上不存在用于执行这项工作的库。
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = "C:\\python\\python.exe";
start.Arguments = $"C:\\python27\\client.py {word}";
start.UseShellExecute = false; // Do not use OS shell
start.CreateNoWindow = true; // We don't need new window
start.RedirectStandardOutput = true; // Any output, generated by application will be redirected back
start.RedirectStandardError = true; // Any error in standard output will be redirected back (for example exceptions)
using (Process process = Process.Start(start))
{
using (StreamReader reader = process.StandardOutput)
{
string stderr = process.StandardError.ReadToEnd(); // Here are the exceptions from our Python script
string output = process.StandardOutput.ReadToEnd();
string result = reader.ReadToEnd(); // Here is the result of StdOut(for example: print "test")
}
}
客户就是这样
client.py