在c#中读取/写入命令行程序

我正在尝试与C#的命令行程序交谈。 它是一种情绪分析器。 它的工作原理如下:

CMD> java -jar analyser.jar

>Starting analyser...

{这是我想从我的c#程序中插入内容的地方。 例如:}

I love you!

{并且程序响应 – 我想读这个}

Very Positive

这是一些伪代码:

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

C#提供哪些方法来写入流程的标准输入并从其标准输出读取?

您要做的是“重定向”您正在启动的进程的标准输入和输出通道。 这将允许您通过C#流读取和写入子进程。

为此,首先需要在用于启动子进程的ProcessStartInfo类中设置RedirectStandardInputRedirectStandardOutput字段:

 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.StandardOutput将成为附加到子进程的输入和输出的StreamReader 。 根据子进程的确切输入和输出,您可能需要小心避免死锁(例如,当其他程序在等待输入时,程序正在等待输出)。 为避免这种情况,您可以使用BeginOutputReadLine方法异步接收输出(如果需要)。

这是一个小演示,展示了如何做你想要的。

这是一个玩具命令行应用程序,只需从标准输入和回声读回标准输出:

 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异步捕获输出(链接文档中有示例代码)。

这不是一般性问题,而是特定于应用程序的问题。 我刚刚碰巧在做同样的事情。 我们可能不应该在这里讨论这个问题,但请原谅我,我只是想提供帮助。

关键是你需要为jar输入正确的参数

 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

希望能帮助到你!