1
嘗試使用TaskScheduler COM Interop lib創建登錄任務。 下面的代碼不會爲TASK_TRIGGER_LOGON
觸發器或TASK_TRIGGER_SESSION_STATE_CHANGE
觸發器創建任務。Taskscheduler COM Interop C# - 登錄任務
//create task scheduler instance
TaskScheduler.TaskScheduler taskService = new TaskScheduler.TaskScheduler();
taskService.Connect();
ITaskDefinition taskDefinition = taskService.NewTask(0);
taskDefinition.Settings.Enabled = true;
taskDefinition.Settings.Compatibility = _TASK_COMPATIBILITY.TASK_COMPATIBILITY_V2_4;
//create trigger for task creation.
ITriggerCollection _iTriggerCollection = taskDefinition.Triggers;
ITrigger _trigger = _iTriggerCollection.Create(_TASK_TRIGGER_TYPE2.TASK_TRIGGER_LOGON);
_trigger.Enabled = true;
///get actions.
IActionCollection actions = taskDefinition.Actions;
_TASK_ACTION_TYPE actionType = _TASK_ACTION_TYPE.TASK_ACTION_EXEC;
//create new action
IAction action = actions.Create(actionType);
IExecAction execAction = action as IExecAction;
execAction.Path = @"calc.exe";
ITaskFolder rootFolder = taskService.GetFolder(@"\");
//register task.
rootFolder.RegisterTaskDefinition("test", taskDefinition, 6, null, null, _TASK_LOGON_TYPE.TASK_LOGON_NONE, null);
即使如此,如果觸發器是TASK_TRIGGER_TIME
,它工作正常,並創建任務。
例如,
//create trigger for task creation.
ITriggerCollection _iTriggerCollection = taskDefinition.Triggers;
ITrigger _trigger = _iTriggerCollection.Create(_TASK_TRIGGER_TYPE2.TASK_TRIGGER_TIME);
_trigger.StartBoundary = DateTime.Now.AddSeconds(15).ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss");
_trigger.EndBoundary = DateTime.Now.AddMinutes(1).ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss");
_trigger.Enabled = true;