I have problem with modifying tasks which already exists on machine. I'm trying to do this with generated interop interfaces from C# (Interop.TaskScheduler.dll generated from system32/taskschd.dll).
To start with, I can't use other libraries like http://taskscheduler.codeplex.com/. Already tested and it works with library mentioned before. Now when I try do same with generated interfaces nothing changes. Basically what I'm doing:
string STR_DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
string taskName = "taskName",
user = "user",
pass = "pass";
DateTime nextRun = new DateTime.Now.AddDays(7);
TaskSchedulerClass ts = new TaskSchedulerClass();
ts.Connect(null, null, null, null);
IRegisteredTask task = ts.GetFolder("\\").GetTask(String.Format("\\{0}",taskName));
foreach (ITrigger t in task.Definition.Triggers)
t.StartBoundary = nextRun.ToString(STR_DateTimeFormat.Replace(" ", "T"));
ts.GetFolder("\\").RegisterTaskDefinition(task.Path,
task.Definition,
(int)_TASK_CREATION.TASK_UPDATE,
user,
pass,
_TASK_LOGON_TYPE.TASK_LOGON_PASSWORD,
null);
For first look it should be working, but for some reason when I try to assign new datetime for run in line:
t.StartBoundary = nextRun.ToString(STR_DateTimeFormat.Replace(" ", "T"));
It doesn't work. Actually in that foreach it's changed but when I tried to debug and created another foreach which prints value of StartBoundary it shows old value. Am I doing something incorrect? Is there any chance to get it working? :-) Thank you.
Clark Bohs
Yes. You have to create a new task definition with the same title and folder to update the existing one. Try to register it and see the task in Task Scheduler. It should have updated the task with the new schedule. But, the history should remain the same.
If you are attempting to update a task using the Task Scheduler C# API like this, you need to declare a new ITaskDefinition. If you attempt to change the existing definition and then re-Register, it does not work.
Answer credit goes to original poster, see comment on question. I wrote up this answer to clarify the issue, and draw attention to the fact that the question had been answered, as a similar issue troubled me for a few days.