1

I've searched for a way to do this but have so far not been successful.

My question is similar to this one: How do I run a Python script from C#?

But in my case, the script goes more like this:

def main():
  i = raw_input("i = ")

I can redirect the output fine by creating a process like the following:

ProcessStartInfo info = new ProcessStartInfo("cmd", "/c python myFile.py");

How would I send input back to it?

Please note, the python script can be anything. I don't know when an input might be expected so using a streamwriter to write to the process by setting redirectStandardInput to true doesn't work..

Any help would be great. Thanks all!

Community
  • 1
  • 1
Nikeah
  • 40
  • 6

1 Answers1

0

You will have to redirect the stdin of your process. Here is a full example of how you can do that:

http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.redirectstandardinput.aspx

chtenb
  • 14,924
  • 14
  • 78
  • 116
  • Unfortunately redirecting stdin doesn't seem to work when cmd is used to run another process like python. In any case, redirection requires the C# code to know inputs are expected, when and how many etc prior to running the script.. – Nikeah Sep 22 '13 at 15:53
  • How exactly did you notice that it doesn't work? Can you provide an example? I'm afraid I don't understand the question – chtenb Sep 22 '13 at 17:52
  • 1
    Thought I'd come back and reply to this as redirecting does work. Turns out that the Python process needs to be run in interactive mode for this to work. Can be done by passing "-i" as an argument to the process. Thanks Chiel – Nikeah Oct 21 '13 at 09:21