簡體   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