使用C#运行交互式命令行exe [英] Run interactive command line exe using c#
本文介绍了使用C#运行交互式命令行exe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我可以使用 process.start()
运行命令行进程。
我可以使用标准输入提供输入。之后,当该过程再次要求用户输入时,我的程序如何知道输入并将其传递给该exe?
I can run a command line process using process.start()
.
I can provide input using standard input. After that when the process demands user input again, how can my program know and pass the input to that exe?
推荐答案
此处的示例听起来与您所需要的相似,使用 Process.StandardInput
和 StreamWriter
。
There's an example here that sounds similar to what you need, using Process.StandardInput
and a StreamWriter
.
Process sortProcess;
sortProcess = new Process();
sortProcess.StartInfo.FileName = "Sort.exe";
// Set UseShellExecute to false for redirection.
sortProcess.StartInfo.UseShellExecute = false;
// Redirect the standard output of the sort command.
// This stream is read asynchronously using an event handler.
sortProcess.StartInfo.RedirectStandardOutput = true;
sortOutput = new StringBuilder("");
// Set our event handler to asynchronously read the sort output.
sortProcess.OutputDataReceived += new DataReceivedEventHandler(SortOutputHandler);
// Redirect standard input as well. This stream
// is used synchronously.
sortProcess.StartInfo.RedirectStandardInput = true;
sortProcess.Start();
// Use a stream writer to synchronously write the sort input.
StreamWriter sortStreamWriter = sortProcess.StandardInput;
// Start the asynchronous read of the sort output stream.
sortProcess.BeginOutputReadLine();
Console.WriteLine("Ready to sort up to 50 lines of text");
String inputText;
int numInputLines = 0;
do
{
Console.WriteLine("Enter a text line (or press the Enter key to stop):");
inputText = Console.ReadLine();
if (!String.IsNullOrEmpty(inputText))
{
numInputLines ++;
sortStreamWriter.WriteLine(inputText);
}
}
希望有帮助
这篇关于使用C#运行交互式命令行exe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文