6

我有一个维护状态的(C#)控制台应用程序。可以通过控制台向应用程序提供各种输入来更改状态。我需要能够为应用程序提供一些输入,然后读取输出冲洗并重复。

我创建了一个新进程并完成了重定向输入/输出的所有正常工作。问题是,在我发送输入并调用ReadLine()标准输出之后,在我调用标准输入之前它没有返回值,Close()之后我无法再写入输入流。

如何在仍接收输出的同时保持打开输入流?

 var process = new Process
                          {
                              StartInfo =
                                  {
                                      FileName =
                                          @"blabal.exe",
                                      RedirectStandardInput = true,
                                      RedirectStandardError = true,
                                      RedirectStandardOutput = true,
                                      UseShellExecute = false,
                                      CreateNoWindow = true,
                                      ErrorDialog = false
                                  }
                          };


        process.EnableRaisingEvents = false;

        process.Start();

        var standardInput = process.StandardInput;
        standardInput.AutoFlush = true;
        var standardOutput = process.StandardOutput;
        var standardError = process.StandardError;

        standardInput.Write("ready");
        standardInput.Close(); // <-- output doesn't arrive before after this line
        var outputData = standardOutput.ReadLine();

        process.Close();
        process.Dispose();

我从中重定向 IO 的控制台应用程序非常简单。它使用从控制台读取Console.Read()并使用Console.Write(). 我确定这些数据是可读的,因为我有另一个应用程序使用标准输出/输入(不是用 .NET 编写)从中读取数据。

4

1 回答 1

4

发生这种情况是因为您正在使用Write("ready")which 会将字符串附加到文本中,而不是使用WriteLine("ready"). 就这么简单:)。

于 2011-07-17T03:41:28.860 回答