重复地将输入输入到流程中标准输入
我有一个(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 编写的)从中读取数据。
I have a (C#) console application which maintains a state. The state can be altered by feeding the application with various input through the console. I need to be able to both feed the application with a bit of input, then read the output rinse and repeat.
I create a new process and do all of the normal work of redirecting the input/output. The problem is that after I've sent input and call ReadLine()
on the standard output it does not return a value before I call Close()
on the standard input after which I cannot write anymore to the input stream.
How can I keep open the input stream while still receiving output?
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();
The console application I'm redirecting IO from is very simple. It reads from the console using Console.Read()
and writes to it using Console.Write()
. I know for certain that this data is readable, since I have another application that reads from it using standard output / input (not written in .NET).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
发生这种情况是因为您使用的是
Write("ready")
,它将向文本附加一个字符串,而不是使用WriteLine("ready")
。就这么简单:)。That is happening because of you are using
Write("ready")
which is will append a string to the text, instead useWriteLine("ready")
. that simple :).