8

I'm learning C# at the moment for a bit of fun and am trying to make a windows application that has a bit of a gui for running some python commands. Basically, I'm trying to teach myself the guts of running a process and sending commands to it, as well as receiving commands from it.

I have the following code at the moment:

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;

Running python.exe from a command prompt gives some introductory text that I'd like to capture and send to a textbox in the windows form (textBox1). Basically, the goal is to have something that looks like the python console running from the windows app. When I don't set UseShellExecute to false, a console pops up and everything runs fine; however, when I set UseShellExecute to false in order to re-direct the input, all I get is that a console pops up very quickly and closes again.

What am I doing wrong here?

2 Answers2

3

For some reason, you shouldn't use forward slashes when you start the process.

Compare (does not work):

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);
}

to (works as expected):

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);
}
Ben Schwehn
  • 4,505
  • 1
  • 27
  • 45
  • Thanks! I can now get it running a python script using this method (plus some of the links supplied by @Chris Haas, but it doesn't seem possible to get the full output from the interactive python exe. – Robert Fisher May 20 '11 at 13:52
  • When you don't use ShellExecute, the API called is `CreateProcess` and that is known not to accept `/` as a directory separator. – David Heffernan May 20 '11 at 16:06
1

Python seems to be doing something weird. I wouldn't believe it until I tested it and then did some research. But all of these posts basically seem to have the same exact problem:

Community
  • 1
  • 1
Chris Haas
  • 53,986
  • 12
  • 141
  • 274
  • 1
    Does it still not work if you change from forward to backward slashes? It makes a difference in my test with cmd, but I don't have python installed, so I can't test it with python... – Ben Schwehn May 20 '11 at 13:28
  • Thanks to both of you for the replies - I've tested the code with backslashes instead, and it still pops up with the console window which then disappears very quickly after. It's very odd indeed! – Robert Fisher May 20 '11 at 13:46
  • 1
    I tested it with Python 2.6 and couldn't get it to work either. I used forward slahes, checked StdErr in addition to StdOut, set the environmental variable `PYTHONUNBUFFERED`, passed `-v` as an argument and even used an async data received handler and couldn't get it to work. – Chris Haas May 20 '11 at 13:54