2014-01-21 41 views
0

我正在試圖與來自c#的命令行程序進行交談。這是一個情緒分析器。它的工作原理是這樣的:讀取/寫入命令行程序中的c#

CMD> Java的罐子analyser.jar

啓動儀...

{這是我想從我的C#程序中插入的東西。例如:}

我愛你!

{而程序響應 - 我想讀這個}

非常積極

下面是一些僞代碼:

Process.start("sentiment.exe"); 
Process.writeline("I love you"); 
string Response = Process.readline(); 

什麼方法呢C#報價爲寫入進程的標準輸入並從其標準輸出中讀取數據?

+0

而你的問題是....? –

+0

你不清楚你在問什麼? – Tommassiov

+0

也許你正在尋找'|'來將一個程序的輸出鏈接到另一個程序,比如'type my.txt |更多'...通常'cmd /?'將解決很多與之相關的問題。 –

回答

3

你要做的是爲你啓動的進程「重定向」標準輸入和輸出通道。這將允許您通過C#流讀取和寫入子進程。

要做到這一點,首先需要設置在ProcessStartInfoRedirectStandardInputRedirectStandardOutput領域用於啓動子進程:

Process p = new Process(); 
p.StartInfo.UseShellExecute = false; 
p.StartInfo.RedirectStandardInput = true; 
p.StartInfo.RedirectStandardOutput = true; 
p.StartInfo.FileName = "otherprogram.exe"; 
p.Start(); 

一旦這樣做了,p.StandardInputp.StandardOutputStreamReader S中的附加到子進程的輸入和輸出。根據子進程的確切輸入和輸出,您可能需要小心以避免死鎖(例如,在其他程序正在等待輸入時,程序正在等待輸出的情況下)。爲了避免這種情況,如果需要,可以使用BeginOutputReadLine方法異步接收輸出。

+0

+1。這是一個很好的起點。正如Michael Edenfield在回答中指出的那樣,必須注意實際閱讀輸出 - 搜索類似問題(即提及'RedirectStandardInput')以獲得完整的樣本/解決方案。 –

0

這裏有一個小小的演示,展示如何去做你所要求的。

這是一個玩具命令行的應用程序,只是從標準輸入和回聲回讀到標準輸出:

class Echoer 
{ 
    static void Main(string[] args) 
    { 
     while (true) 
     { 
      var input = Console.ReadLine(); 
      Console.WriteLine("Echoing: " + input); 
     } 
    } 
} 

這是另一種運行上述應用程序,使輸入給它的命令行的應用程序,以及讀取其輸出:

class Program 
{ 
    static void Main(string[] args) 
    { 
     ProcessStartInfo processStartInfo; 
     processStartInfo = new ProcessStartInfo(); 
     processStartInfo.CreateNoWindow = true; 

     // Redirect IO to allow us to read and write to it. 
     processStartInfo.RedirectStandardOutput = true; 
     processStartInfo.RedirectStandardInput = true; 
     processStartInfo.UseShellExecute = false; 

     processStartInfo.FileName = @"path\to\your\Echoer.exe"; 

     Process process = new Process(); 
     process.StartInfo = processStartInfo; 
     process.Start(); 
     int counter = 0; 
     while (counter < 10) 
     { 
      // Write to the process's standard input. 
      process.StandardInput.WriteLine(counter.ToString()); 

      // Read from the process's standard output. 
      var output = process.StandardOutput.ReadLine(); 
      Console.WriteLine("Hosted process said: " + output); 
      counter++; 
     } 

     process.Kill(); 

     Console.WriteLine("Hit any key to exit."); 
     Console.ReadKey(); 
    } 
} 

如果你正在主持不是簡單地產生一個線路輸出的響應線路輸入的應用程序,那麼你可以看看異步使用Process.BeginOutputReadLine捕獲輸出(有示例代碼鏈接的文檔中)。

0

這不是一個普遍的問題,而是一個特定於應用程序的問題。我碰巧正在做同樣的事情。我們可能不應該在這裏討論這個問題,但是請原諒我,我只是想幫忙。

關鍵的一點是,你需要輸入正確的論據,罐子

var processInfo = new ProcessStartInfo("java.exe", "-jar SentiStrengthCom.jar stdin sentidata data201109/ scale"){ ... } 

下面是一個有效的解決方案:

// Start the java server, probably in a background thread 
    void bgw_DoWork(object sender, DoWorkEventArgs e) 
    { 
     string directory = ...; // the directory of jar 

     var processInfo = new ProcessStartInfo("java.exe", "-jar SentiStrengthCom.jar stdin sentidata data201109/ scale") 
     { 
      // Initialize properties of "processInfo" 
      CreateNoWindow = true, 
      UseShellExecute = false, 
      WorkingDirectory = directory, 
      RedirectStandardOutput = true, 
      RedirectStandardError = true, 
      RedirectStandardInput = true, 
     }; 

     JavaSentiStrength = new Process(); 
     JavaSentiStrength.StartInfo = processInfo; 

     JavaSentiStrength.Start(); 

     Console.WriteLine("SentiStrengthCom Java is running..."); 

     JavaSentiStrength.WaitForExit(); 

     int exitCode = 0; 
     if (JavaSentiStrength != null) exitCode = JavaSentiStrength.ExitCode; 
     Debug.WriteLine("Java SentiStrengthCom closed down! " + exitCode); 
     JavaSentiStrength.Close(); 
     JavaSentiStrength = null; 

    } 

    // A button click 
    private void btnRequest_Click(object sender, EventArgs e) 
    { 
     JavaSentiStrength.StandardInput.WriteLine("love you "); 
     string s_out = JavaSentiStrength.StandardOutput.ReadLine(); 
     Debug.WriteLine("SentiStrengthCom output: " + s_out); 
    } 

輸出是

SentiStrengthCom輸出:3 -1 2

希望它有幫助!