Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

External Process Execution

tango.sys.Process provides an easy to use, cross-platform class for executing external applications from within your program, and reading their output should you need. Let's jump right in. Here we have an example that lists the current directory through an external program.

private import tango.io.Stdout;
private import tango.sys.Process;
private import tango.core.Exception;

/**
 * Example program for the tango.sys.Process class.
 */
void main()
{
    version (Windows)
        char[] command = "cmd.exe /c dir";
    else version (Posix)
        char[] command = "ls -l";
    else
        assert(false, "Unsupported platform");

    try
    {
        auto p = new Process(command, null);
        Stdout.formatln ("Executing {}", p.toString);
        p.execute;

        Stdout.formatln ("Output from process: {} (pid {})\n---", 
                         p.programName, p.pid);

        Stdout.copy (p.stdout);
        Stdout ("---\n");

        auto result = p.wait();
        Stdout.formatln ("Process '{}' ({}) finished: {}",
                         p.programName, p.pid, result.toString);
    }
    catch (ProcessException e)
    {
        Stdout.formatln ("Process execution failed: {}", e.toString);
    }
}