C# 从winform button事件函数调用时未创建子进程?
我正在尝试从我的UI运行python代码。我正在使用下面的代码来执行此操作C# 从winform button事件函数调用时未创建子进程?,c#,python,winforms,C#,Python,Winforms,我正在尝试从我的UI运行python代码。我正在使用下面的代码来执行此操作 Process p = new Process(); string cmd = @"python filepath & exit"; p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.F
Process p = new Process();
string cmd = @"python filepath & exit";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.RedirectStandardInput = true;
p.Start();
StreamWriter myStreamWriter = p.StandardInput;
myStreamWriter.WriteLine(cmd.ToString());
myStreamWriter.Close();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
Console.WriteLine(output);
Console.ReadLine();
上面的代码打开一个cmd提示符并执行python文件。
当我使用控制台应用程序测试它时,该代码运行良好,但当我在UI应用程序中的“run”按钮的事件函数中使用它时,它不起作用。有什么特别的原因吗?如果将StartInfo.UseShellExecute设置为true,会发生什么情况?我知道会出现一个cmd窗口,但python可能需要shell才能正常运行。@DavidArno如果我将startinfo.useshellexecute设置为true,那么我将无法重定向输入和输出。说得好。很明显,我没有看透这一点。对不起。:)好的,那么当你说“当我在事件函数中使用它时,它不起作用”时,它以什么方式不起作用?该方法是否被调用?你有例外吗?输出是否设置为任何值?@DavidArno我没有任何异常……我只是得到一个空白的控制台窗口,没有显示我运行的程序的输出。