2016-09-12 4 views
1

C#Processクラスからcmd.exeにコマンドを送信できないという問題があります。複数のプロセスコマンドで応答が返されない

基本的には、R.exeを呼び出してから、いくつかのR関数を実行して結果を取り出す前にデータをステージングするコマンドを送信します。

しかし、私は戻って、単純な 'DIR' statemenet :(から結果を得ることができません

Process p = new Process(); 
ProcessStartInfo info = new ProcessStartInfo(); 
info.FileName = "cmd.exe"; 
info.RedirectStandardInput = true; 
info.RedirectStandardOutput = true; 
info.UseShellExecute = false; 
string pathToR = @"C:\Program Files\R\R-3.3.1\bin"; 

p.StartInfo = info; 
p.Start(); 

List<string> output = new List<string>(); 
using (StreamWriter sw = p.StandardInput) 
{ 
    if (sw.BaseStream.CanWrite) 
    { 
     sw.WriteLine("cd " + pathToR); 
     sw.WriteLine("dir"); 


     while (p.StandardOutput.Peek() > -1) 
     { 
      var peekVal = p.StandardOutput.Peek(); 
      output.Add(p.StandardOutput.ReadLine()); 
     } 
    } 
} 
foreach (var line in output) 
{ 
    Console.WriteLine(line); 
} 

p.StandardInput.Close(); 
p.StandardOutput.Close(); 
p.WaitForExit(); 
p.Close(); 

Console.ReadLine(); 

出力:私はこの結果にいくつかのバリエーションを見てきました

Microsoft Windows [Version 10.0.10586] 
(c) 2015 Microsoft Corporation. All rights reserved. 

c:\users\micah_000\documents\visual studio 2015\Projects\RStagingTestApp\RStagingTestApp\bin\Debug>cd C:\Program Files\R\R-3.3.1\bin 
The process tried to write to a nonexistent pipe. 
The process tried to write to a nonexistent pipe. 
The process tried to write to a nonexistent pipe. 
The process tried to write to a nonexistent pipe. 
The process tried to write to a nonexistent pipe. 

が、私(

+0

おそらく、stdoutの非同期読み取りを試してみてください。私は理由を考え出すことはありませんでしたが、私はcmd.exeに直接コマンドを送信してプレイした最後の時間に、私は[ここ]で見られるように非同期読み取りを使用しなければなりませんでした(http://stackoverflow.com/questions/38490894/not-able-to- send-commands-to-cmd-exe-process/38491255#38491255)、同期バージョンが特に機能しなかったためです。 Rや他のプログラムと同様の問題かもしれません。 – Quantic

答えて

0

多くの場所で非同期読み取りが最も効果的だと言われていますが、説得力のある包括的な説明がありますが、私はそれを手に入れることができませんでした〜へ私のためのrk。

This 1つが動作するように見えました。 AutoFlushをtrueに設定している可能性があります。または最後に一度だけ読んでください。

関連する問題