.NET – Program Glue

C 的 stdio.h 有 popen/pclose 可以用,python 有 popen/popen2… ,perl 可以把程式當作檔案代號直接開啟執行,這些都可以做到程式間黏合、互動,那 .NET 要怎麼作呢?

可以用 System.Diagnostics.Process 來做到一樣的功能:

using System;
using System.Diagnostics;

public module T {
    public Main (args: array[string]) : void
    {
        def proc = Process ();
        def proc_info = ProcessStartInfo (args[0]);

        proc_info.CreateNoWindow = true;
        proc_info.UseShellExecute = false;
        proc_info.RedirectStandardInput = true;
        proc_info.RedirectStandardOutput = true;
        proc_info.RedirectStandardError = true;
        proc.StartInfo = proc_info;

        proc.Start ();
        while (true) {
            proc.StandardInput.WriteLine (Console.ReadLine ());
            Console.WriteLine (proc.StandardOutput.ReadLine ());
        }
    }
}