C# 为什么Shellexecute=false会破坏这一点?

C# 为什么Shellexecute=false会破坏这一点?,c#,winforms,C#,Winforms,我现在学习C#是为了找点乐趣,我正在尝试制作一个windows应用程序,它有一个gui,可以运行一些python命令。基本上,我试图教会自己运行流程、向流程发送命令以及从流程接收命令的勇气 我目前有以下代码: Process p = new Process(); p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.FileName = "C:/Python31/py

我现在学习C#是为了找点乐趣,我正在尝试制作一个windows应用程序,它有一个gui,可以运行一些python命令。基本上,我试图教会自己运行流程、向流程发送命令以及从流程接收命令的勇气

我目前有以下代码:

Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "C:/Python31/python.exe";
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
textBox1.Text = output;
从命令提示符下运行python.exe将提供一些介绍性文本,我希望捕获这些文本并将其发送到windows窗体中的文本框(textBox1)。基本上,我们的目标是在windows应用程序中运行类似python控制台的东西。当我没有将UseShellExecute设置为false时,会弹出一个控制台,一切正常;然而,当我将UseShellExecute设置为false以重新引导输入时,我得到的只是一个控制台很快弹出并再次关闭


我做错了什么?

Python似乎在做一些奇怪的事情。我不会相信,直到我测试了它,然后做了一些研究。但所有这些帖子基本上都有相同的问题:


由于某些原因,启动流程时不应使用正斜杠

比较(不起作用):

(按预期工作):


如果从正向斜杠改为反向斜杠,它仍然不起作用吗?在我使用cmd进行测试时会有所不同,但我没有安装python,所以我无法使用python进行测试…多亏了你们两位的回复-我用反斜杠测试了代码,它仍然会弹出控制台窗口,然后很快消失。真奇怪!我用Python2.6对它进行了测试,但也无法让它工作。我使用了前向slahes,除了StdOut之外还检查了StdErr,设置了环境变量
pythonunbuffer
,将
-v
作为参数传递,甚至使用了异步数据接收处理程序,但无法使其工作。谢谢!现在,我可以使用此方法运行python脚本(加上@Chris Haas提供的一些链接,但似乎不可能从交互式python exe获得完整的输出。当您不使用ShellExecute时,调用的API是
CreateProcess
,并且已知它不接受
作为目录分隔符。
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.CreateNoWindow = true;

p.StartInfo.FileName = "C:/windows/system32/cmd.exe";
p.StartInfo.Arguments = "/c dir" ;
p.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);
bool f = p.Start();
p.BeginOutputReadLine();
p.WaitForExit();


[...]


static void p_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
    Console.WriteLine(e.Data);
}
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.CreateNoWindow = true;

p.StartInfo.FileName = @"C:\windows\system32\cmd.exe";
p.StartInfo.Arguments = "/c dir" ;
p.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);

bool f = p.Start();
p.BeginOutputReadLine();
p.WaitForExit();


[...]

static void p_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
    Console.WriteLine(e.Data);
}