控制台应用程序串行执行

时间:2012-05-15 21:47:41

标签: c# xml

我想链接'n'个可执行文件,'n-1'exe的输出作为'n'th exe的输入传递。我计划使用XML文件来配置可执行文件位置,路径,i / p,o / p等。

我的问题是当有多个输出和多个输入(命令行参数)时如何将'n-1'链接到'n'。 我对它有一些想法,但想看看别人的想法,也许我会知道一个有效/快速的方法来做到这一点。灵活的xml配置的设计模式会有所帮助。

我将使用的伪XML结构

<executables>
  <entity position="1" exePath="c:\something1.exe">
     <op><name="a" value=""></op>
  </entity>
  <entity position="2" exePath="c:\something2.exe">
   <ip><name="a"></ip>
   <op><name="b"  value=""></op>
  </entity>
  <entity position="3" exePath="c:\something3.exe">
   <ip><name="b"</ip>
  </entity>
</executables>

在配置这些之前,我会了解i / p和o / p。上下文是我可能会或可能不会包含我将使用的某些链接类型中的某些节点,从而有效地创建灵活的串行exe执行路径。

1 个答案:

答案 0 :(得分:1)

您可以使用System.Diagnostics.Process类。以下代码应该为两个可执行文件提供技巧:

using (Process outerProc = new Process())
{
    outerProc.StartInfo.FileName = "something1.exe";
    outerProc.StartInfo.UseShellExecute = false;
    outerProc.StartInfo.RedirectStandardOutput = true;
    outerProc.Start();

    string str = outerProc.StandardOutput.ReadToEnd();

    using(Process innerProc = new Process())
    {
        innerProc.StartInfo.FileName = "something2.exe";
        innerProc.StartInfo.UseShellExecute = false;
        innerProc.StartInfo.RedirectStandardInput = true;
        innerProc.Start();

        innerProc.StandardInput.Write(str);
        innerProc.WaitForExit();
    }

    outerProc.WaitForExit();
}

您可以轻松修改它以适合您的“n-1”到“n”情况。