简体   繁体   English

使用c#运行交互式命令行exe

[英]Run interactive command line exe using c#

I can run a command line process using process.start() . 我可以使用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? 之后,当进程再次要求用户输入时,我的程序如何知道并将输入传递给该exe?

There's an example here that sounds similar to what you need, using Process.StandardInput and a StreamWriter . 这里有一个例子 ,听起来与你需要的相似,使用Process.StandardInputStreamWriter

        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);
            }
        }

hope that helps 希望有所帮助

If I understand correctly I think you should be able to do this by redirecting the Standard Input/Output using RedirectStandardOutput and RedirectStandardInput . 如果我理解正确,我认为您应该可以通过使用RedirectStandardOutputRedirectStandardInput重定向标准输入/输出来实现此目的。

The WinSCP project has a C# sample for how to communicate with it using this way. WinSCP项目有一个C#示例,用于使用这种方式与它进行通信。 You can find it here: SFTP file transfers in .NET (though this sample only collects the output without using it at all, but the technique should be the same.) 你可以在这里找到它: 在.NET中的SFTP文件传输 (尽管这个示例只收集输出而根本不使用它,但技术应该是相同的。)

You want to look up IPC. 你想查找IPC。 As robert showed above, the Process class in .NET will help you. 正如罗伯特上面所示,.NET中的Process类可以帮助你。 But specifically for your problem (how to know when to write data): You can't. 但专门针对您的问题(如何知道何时写入数据):您不能。 Not generically. 不是一般的。

If you know the required input (eg "yyy"), you can supply it in the STDIN of the created process. 如果您知道所需的输入(例如“yyy”),则可以在创建的过程的STDIN中提供它。 You don't have to wait for the program to request this information: It will just read from STDIN when it wants data. 您不必等待程序请求此信息:它只是在需要数据时从STDIN读取。

If you need to process the programs output to decide what to write to STDIN , try reading the processes STDOUT . 如果需要处理程序输出以决定写入STDIN ,请尝试读取STDOUT进程。 You might run into problems with flushing, though... 你可能会遇到冲洗问题,但......

暂无
暂无

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

相关问题 C#命令行项目-通过.exe运行时出错 - C# command line project - Error when run via .exe 为什么命令行C#Interactive(csi.exe)会忽略显式程序集参考? - Why Does Command Line C# Interactive (csi.exe) Ignore Explicit Assembly Reference? C#中带参数执行Command line.exe - Executing Command line .exe with parameters in C# 如何在 c# 中使用命令提示符运行 deepfreeze 命令行 - How to run deepfreeze command line using command prompt in c# 如何创建命令行来运行c#exe文件并为form1中的函数提供一些参数? - How to create a command line to run c# exe file and give some parameters to a function in form1? C# 控制台应用程序:有没有办法检测 exe 是否从命令行运行? - C# Console Application: Is there a way to detect whether the exe was run from a command line or not? 使用 C# 以编程方式运行命令行代码 - Run command line code programmatically using C# 如何使用C#.net从命令行调用具有多个参数的C ++ exe - How to call C++ exe with multiple parameters from command line using C# .net 我试图从C#Windows应用程序打开命令行以运行exe文件,但它仅打开cmd而不运行命令 - Im trying to open a command line from c# windows application in order to run an exe file but its only opening the cmd and not running the command 在C#程序中使用包含的命令行exe:路径是什么? - Using an included command line exe in a C# program: what's the path?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM