Kanru’s 探險日誌

當發現美好的事物時,所要做的第一件事,就是把它分享給所有人

.NET - Program Glue

leave comments »

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

Written by Kanru Chen

December 25th, 2005 at 7:55 pm

Posted in Programming

Tagged with ,

Leave a Reply