Click or drag to resize

Set host working directory

Problem

You want to set the working directory of the host.

Solution

To set the host's working directory, set WorkingDirectory and pass the path of the desired working directory. The string will be resolved by expanding the string as an environment variable. For instance, "%TEMP%" will resolve to the full temporary directory path. The task will fail if the directory does not exist on the agent machine.

C#
using System;
using System.IO;
using AGI.Parallel.Client;
using AGI.Parallel.Infrastructure;

namespace CodeSamples
{
    class Program
    {
        static void Main(string[] args)
        {
            using (IJobScheduler scheduler = new ClusterJobScheduler("localhost"))
            {
                scheduler.Connect();
                Job job = scheduler.CreateJob();
                job.AddTask(new ReturnsWorkingDirectory());

                // Use the Windows variables to specify specific directories.
                job.TaskEnvironment.WorkingDirectory = "%TEMP%";

                job.Submit();
                job.WaitUntilDone();

                Console.WriteLine("Working Directory: " + job.Tasks[0].Result);
            }

            /*
             * The output of the application should resemble:
             * Working Directory: C:\Users\jdoe\AppData\Local\Temp
             */
        }

        [Serializable]
        class ReturnsWorkingDirectory : Task
        {
            public override void Execute()
            {
                this.Result = Directory.GetCurrentDirectory();
            }
        }
    }
}
Discussion

By default the working directory of the host process is the agent bin folder. There are some cases where a task needs a user defined working directory. The most common of these cases is that the application assumes a relative path to data. Sometimes this can be avoided by modifying the application itself. However, setting the working directory is an option in cases where modifying the application is not possible.

See Also

Reference

ExpandEnvironmentVariables

STK Parallel Computing Server 2.9 API for .NET