2014-01-21 42 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

希望它有帮助!