将一个进程对象的标准输出重定向到另一个进程对象的标准输入

发布于 2024-08-03 20:17:37 字数 521 浏览 2 评论 0原文

如何设置两个外部可执行文件从 ac# 应用程序运行,其中第一个应用程序的标准输出从第二个应用程序路由到标准输入?

我知道如何使用 Process 对象来运行外部程序,但我没有看到类似“myprogram1 -some -options | myprogram2 -some -options”之类的方法。我还需要捕获第二个程序的标准输出(示例中为 myprogram2)。

在 PHP 中,我会这样做:

$descriptorspec = array(
            1 => array("pipe", "w"),  // stdout
        );

$this->command_process_resource = proc_open("myprogram1 -some -options | myprogram2 -some -options", $descriptorspec, $pipes);

$pipes[1] 将是链中最后一个程序的标准输出。有没有办法在 C# 中实现这一点?

How can I set up two external executables to run from a c# application where stdout from the first is routed to stdin from the second?

I know how to run external programs by using the Process object, but I don't see a way of doing something like "myprogram1 -some -options | myprogram2 -some -options". I'll also need to catch the stdout of the second program (myprogram2 in the example).

In PHP I would just do this:

$descriptorspec = array(
            1 => array("pipe", "w"),  // stdout
        );

$this->command_process_resource = proc_open("myprogram1 -some -options | myprogram2 -some -options", $descriptorspec, $pipes);

And $pipes[1] would be the stdout from the last program in the chain. Is there a way to accomplish this in c#?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

假面具 2024-08-10 20:17:37

下面是将一个进程的标准输出连接到另一个进程的标准输入的基本示例。

Process out = new Process("program1.exe", "-some -options");
Process in = new Process("program2.exe", "-some -options");

out.UseShellExecute = false;

out.RedirectStandardOutput = true;
in.RedirectStandardInput = true;

using(StreamReader sr = new StreamReader(out.StandardOutput))
using(StreamWriter sw = new StreamWriter(in.StandardInput))
{
  string line;
  while((line = sr.ReadLine()) != null)
  {
    sw.WriteLine(line);
  }
}

Here's a basic example of wiring the standard output of one process to the standard input of another.

Process out = new Process("program1.exe", "-some -options");
Process in = new Process("program2.exe", "-some -options");

out.UseShellExecute = false;

out.RedirectStandardOutput = true;
in.RedirectStandardInput = true;

using(StreamReader sr = new StreamReader(out.StandardOutput))
using(StreamWriter sw = new StreamWriter(in.StandardInput))
{
  string line;
  while((line = sr.ReadLine()) != null)
  {
    sw.WriteLine(line);
  }
}
一个人练习一个人 2024-08-10 20:17:37

您可以使用 System.Diagnostics.Process 类创建 2 个外部进程,并通过 StandardInput 和 StandardOutput 属性将输入和输出粘在一起。

You could use the System.Diagnostics.Process class to create the 2 external processes and stick the in and outs together via the StandardInput and StandardOutput properties.

魄砕の薆 2024-08-10 20:17:37

使用 System.Diagnostics.Process 启动每个进程,并在第二个进程中将 RedirectStandardOutput 设置为 true,并将第一个 RedirectStandardInput 设置为 true。最后将第一个的 StandardInput 设置为第二个的 StandardOutput 。您需要使用 ProcessStartInfo 来启动每个进程。

以下是其中一个重定向示例

Use System.Diagnostics.Process to start each process, and in the second process set the RedirectStandardOutput to true, and the in the first RedirectStandardInput true. Finally set the StandardInput of the first to the StandardOutput of the second . You'll need to use a ProcessStartInfo to start each process.

Here is an example of one of the redirections.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文