【问题标题】:Run interactive command line exe using c#使用 c# 运行交互式命令行 exe
【发布时间】:2010-07-22 11:52:52
【问题描述】:

我可以使用process.start() 运行命令行进程。 我可以使用标准输入提供输入。之后,当进程再次要求用户输入时,我的程序如何知道输入并将其传递给该 exe?

【问题讨论】:

  • 您可能对this post 感兴趣,其中涵盖了使用 .NET 进程的许多复杂性,尤其是在处理输入和输出方面

标签: c#


【解决方案1】:

使用Process.StandardInputStreamWriter,有一个example here 听起来与您需要的相似。

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

希望对你有帮助

【讨论】:

  • 再次感谢。但这是特定于 sort.exe 我在运行时为每个用户生成 exe。我不知道他会输入哪个程序。
  • 我认为您可以使用“cmd.exe”代替他们选择的可执行文件。这是一个 hack,但它可能会完成工作
  • 亲爱的 robert 可以请您解释一下如何使用 cmd.exe 来解决我的问题
  • sortProcess.StartInfo.FileName = "cmd.exe"; 可能会成功
  • 我更新了 my stupid-simple .NET library project 以根据您的回答运行命令行程序以包括对“标准输入输入”的支持 - 谢谢!
【解决方案2】:

如果我理解正确,我认为您应该能够通过使用 RedirectStandardOutputRedirectStandardInput 重定向标准输入/输出来做到这一点。

WinSCP 项目有一个 C# 示例,说明如何使用这种方式与之通信。你可以在这里找到它:SFTP file transfers in .NET(虽然这个示例只收集了输出,根本没有使用它,但技术应该是一样的。)

【讨论】:

  • 感谢您的回复。我尝试过这个。但是要求用户输入的过程不是预定义的。场景就像 1. 我想为 c# 设计在线编译器 2. 我可以使用代码 dom n cretae exe 构建程序 3. 现在我想通过 c# 运行该 exe 4. 用户可以编写需要用户的任何类型的程序在 diff 阶段输入。希望你理解整个场景
  • @rajshades:我原以为会是类似的,只是不是一次性发送所有命令,而是发送一个命令,读取输出,然后发送下一个命令等。虽然我没有测试过这个。不过,作为一般说明,如果您正在创建 exe,您不能尝试给它一些更好的通信方式(WCF、TCP/IP 套接字、名称管道等)吗?
【解决方案3】:

您要查找 IPC。正如上面的 robert 所示,.NET 中的 Process 类将为您提供帮助。但专门针对您的问题(如何知道何时写入数据):您不能。不是通用的。

如果您知道所需的输入(例如“yyy”),您可以在创建的进程的STDIN 中提供它。您不必等待程序请求此信息:它会在需要数据时从STDIN 读取。

如果您需要处理程序输出来决定写入STDIN 的内容,请尝试读取进程STDOUT。不过,您可能会遇到冲洗问题...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-12
    • 2010-09-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多