5

I am trying to integrate a scheduled job statement into Process.Start

Process.Start("schtasks.exe", "\"" + textBox1.Text + "\"");

How would it be possible to add the parameters below into the Process.Start statement above?

schtasks /Create /SC DAILY /TN TestJob /TR "C:\Program Files\test\test.exe 'C:\'"
1

2 Answers 2

11

You can interact with the windows task manager directly using TaskScheduler. It will give you access to a whole range of properties of the task and under what conditions it will be fired. It, of course, require more code, but it gives you all the control that you need in a managaged manner.

This is a piece of code that im using myself and it is working well (ive cut away some of my business logic so not all arguments will compile/make sense). It will basically create a task that will fire one minute from Now:

    TaskScheduler.TaskScheduler scheduler = new TaskScheduler.TaskScheduler();
    scheduler.Connect(null, null, null, null); //run as current user.

    ITaskDefinition taskDef = scheduler.NewTask(0);
    taskDef.RegistrationInfo.Author = "Me me me";
    taskDef.RegistrationInfo.Description = "My description";
    taskDef.Settings.ExecutionTimeLimit = "PT10M"; // 10 minutes
    taskDef.Settings.DisallowStartIfOnBatteries = false;
    taskDef.Settings.StopIfGoingOnBatteries = false;
    taskDef.Settings.WakeToRun = true;

    ITimeTrigger trigger = (ITimeTrigger)taskDef.Triggers.Create(_TASK_TRIGGER_TYPE2.TASK_TRIGGER_TIME);

    DateTime nextRun = DateTime.Now.AddMinutes(1); // one minute from now
    trigger.StartBoundary = nextRun.ToString("s", System.Globalization.CultureInfo.InvariantCulture);

    IExecAction action = (IExecAction)taskDef.Actions.Create(_TASK_ACTION_TYPE.TASK_ACTION_EXEC);
    action.Id = "exe name";
    action.Path = "path to exe";
    action.WorkingDirectory = "working dir";
    action.Arguments = "app arguments";  /// <-- here you put your arguments..

    ITaskFolder root = scheduler.GetFolder("\\");

    IRegisteredTask regTask = root.RegisterTaskDefinition(
        "My task name",
        taskDef,
        (int)_TASK_CREATION.TASK_CREATE_OR_UPDATE,
        null, // user
        null, // password
        _TASK_LOGON_TYPE.TASK_LOGON_INTERACTIVE_TOKEN, //User must already be logged on. The task will be run only in an existing interactive session.
        "" //SDDL
        );

More explaination and code samples can be found here: Calling the Task Scheduler in Windows Vista (and Windows Server 2008) from managed code

Sign up to request clarification or add additional context in comments.

2 Comments

I think this is a little overkill for what I need to do
@James, avoid calling other exes if you can. While that interface is well-defined, proper erro-handling is quite a bit of work.
1

The problem is you need to double-escape.

The proper command at the command line for:

schtasks /Create /SC DAILY /TN TestJob /TR "C:\Program Files\test\test.exe 'C:\'"

is

schtasks /Create /SC DAILY /TN TestJob /TR "\"C:\\Program Files\\test\\test.exe\" \"C:\\\""

So that means you'll need:

Process.Start("schtasks.exe", string.Format(@"/Create /SC DAILY /TN TestJob /TR """"{0}"" ""{1}""""", textBox1.Text.Replace("\", "\\"), @"C:\"));

(Don't have a compiler handy so there may be typos, but you should get the idea.) I'm making the assumption that textBox contains the path to the exe, not sure where the parameter is coming from or if it's hard coded.

1 Comment

Hi, thanks, but the textbox is acutally meant to be nested in-between the "DAILY" and "/TN"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.