繁体   English   中英

在C#中对命令行程序进行读/写

[英]Reading/Writing to a command line program in 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

希望能帮助到你!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM