我是C#新手,我曾尝试使用ProcessStartInfo
类执行一个python程序。我跟踪了this link 并做了同样的事情。但是,我无法获得任何重定向输出。我尝试使用参数执行Python解释器,没有任何问题。然而,我看到控制台弹出,然后立即消失。没有输出打印。我尝试使用方法process.waitForExit(
),但是控制台也消失了。有什么想法吗?
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = @"C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\python.exe";
start.Arguments = @"C:\Users\test\test.py";
start.UseShellExecute = false;
start.RedirectStandardOutput = true;
start.RedirectStandardError = true;
using (Process process = Process.Start(start))
{
using (StreamReader reader = process.StandardOutput)
{
string result = reader.ReadToEnd();
Console.Write(result);
process.WaitForExit();
}
}
发布于 2018-07-01 02:53:32
如果您从VS启动应用程序,则VS在完成执行后关闭应用程序控制台。尝试将Console.ReadLine()
添加到程序的最后一行。
using (Process process = Process.Start(start))
{
using (StreamReader reader = process.StandardOutput)
{
string result = reader.ReadToEnd();
Console.Write(result);
}
}
Console.ReadLine();
https://stackoverflow.com/questions/51116426
复制相似问题