将一个exe的输出重定向到另一个exe: c#

本文关键字:exe 重定向 另一个 输出 一个 | 更新日期: 2023-09-27 18:11:30

我创建了两个简单的.exe文件。其中一个在运行时接受文件名参数,并将文件的内容读入控制台。另一个等待其控制台的输入,然后读取它;现在它只需要打印到控制台,但最终我必须将读取文本重定向到一个新的txt文件。我的问题是,我如何将第一个exe的输出重定向到第二个exe的控制台,在那里可以读取?

提前感谢您提供的任何帮助!:)

屁股的

将一个exe的输出重定向到另一个exe: c#

您可以在命令行中使用管道重定向操作符:

ConsoleApp1.exe | ConsoleApp2.exe

管道操作符将控制台输出从第一个应用程序重定向到第二个应用程序的标准输入。您可以在这里找到更多信息(该链接适用于XP,但规则也适用于Windows Vista和Windows 7)。

From MSDN

// Start the child process.
 Process p = new Process();
 // Redirect the output stream of the child process.
 p.StartInfo.UseShellExecute = false;
 p.StartInfo.RedirectStandardOutput = true;
 p.StartInfo.FileName = "Write500Lines.exe";
 p.Start();
 // Do not wait for the child process to exit before
 // reading to the end of its redirected stream.
 // p.WaitForExit();
 // Read the output stream first and then wait.
 string output = p.StandardOutput.ReadToEnd();
 p.WaitForExit();

你可以逐行读取

///...
string output;
while( ( output = p.StandardOutput.ReadLine() ) != null )
{
    Console.WriteLine(output);
}
p.WaitForExit();