2016-06-25 10 views
1

私はいくつかの引数をつけてPythonスクリプトを実行するC#プログラムを作成しています。エラーがなくProcess ExitCodeが0(デバッガを介してチェック)であっても、プログラムは実行されません(メッセージを出力するだけでなく、ファイルに書き込むことも想定されています)。どこが間違っていますか?C#からpythonスクリプトを呼び出すとエラーがスローされずに呼び出されない

static private string ExecutePython(string sentence) { 
     // full path of python interpreter 
     string python = @"C:\Python27\python.exe"; 

     // python app to call 
     string myPythonApp = @"C:\Users\user_name\Documents\pos_edit.py"; 

     // Create new process start info 
     ProcessStartInfo myProcessStartInfo = new ProcessStartInfo(python); 

     // make sure we can read the output from stdout 
     myProcessStartInfo.UseShellExecute = false; 
     myProcessStartInfo.RedirectStandardOutput = true; 


     myProcessStartInfo.Arguments = string.Format("{0} {1}", myPythonApp, sentence); 

     Process myProcess = new Process(); 
     // assign start information to the process 
     myProcess.StartInfo = myProcessStartInfo; 

     // start process 
     myProcess.Start(); 

     // Read the standard output of the app we called. 
     StreamReader myStreamReader = myProcess.StandardOutput; 
     string myString = myStreamReader.ReadToEnd(); 

     // wait exit signal from the app we called 
     myProcess.WaitForExit(); 

     // close the process 
     myProcess.Close(); 

     return myString; 
} 
+0

"user_name"にスペースが含まれていませんか? – Aya

+0

[c#のpythonスクリプトを実行する]の可能な複製(http://stackoverflow.com/questions/11779143/run-a-python-script-from-c-sharp) – Mate

+0

@Aya no。しかし、私はstdoutを取ることなく仕事をする選択肢を得ました。 – goluhaque

答えて

1

あなたは間違った場所にmyProcess.WaitForExit();を入れました。 のPythonスクリプトを実行したまでを待つ:

... 
myProcess.Start(); 

StreamReader myStreamReader = myProcess.StandardOutput; 

// first, wait to complete 
myProcess.WaitForExit(); 

// only then read the results (stdout) 
string myString = myStreamReader.ReadToEnd(); 
... 
0

は私と一緒に完全に正常に動作します。私はあなたのuser_nameにスペースを含む何かがあると思います。

関連する問題