# 查看系统下所有的任务计划程序

TaskSchedulerClass ts = new TaskSchedulerClass();
ts.Connect(null, null, null, null);
for (int i = 1; i <= tasks_exists.Count; i++)
{

}


# 创建自定义任务计划程序

## 1.实例化对象

//实例化任务对象
scheduler.Connect(null, null, null, null);//连接

## 2.设置基本属性

ITaskDefinition task = scheduler.NewTask(0);
task.RegistrationInfo.Description = "http://blog.csdn.net/bluceyoung";//描述

## 3.设置触发器

ITimeTrigger tt = (ITimeTrigger)task.Triggers.Create(_TASK_TRIGGER_TYPE2.TASK_TRIGGER_TIME);
tt.Repetition.Interval = "PT30M";//循环时间
tt.StartBoundary = "2013-01-21T14:27:25";//开始执行时间

### Interval属性

The amount of time between each restart of the task. The format for this string is P<days>DT<hours>H<minutes>M<seconds>S (for example, "PT5M" is 5 minutes, "PT1H" is 1 hour, and "PT20M" is 20 minutes). The maximum time allowed is 31 days, and the minimum time allowed is 1 minute.

### StartBoundary属性

The date and time must be in the following format: YYYY-MM-DDTHH:MM:SS(+-)HH:MM. The (+-)HH:MM section of the format defines a certain number of hours and minutes ahead or behind Coordinated Universal Time (UTC). For example the date October 11th, 2005 at 1:21:17 with an offset of eight hours behind UTC would be written as 2005-10-11T13:21:17-08:00. If Z is specified for the UTC offset (for example, 2005-10-11T13:21:17Z), then the no offset from UTC will be used. If you do not specify any offset time or Z for the offset (for example, 2005-10-11T13:21:17), then the time zone and daylight saving information that is set on the local computer will be used. When an offset is specified (using hours and minutes or Z), then the time and offset are always used regardless of the time zone and daylight saving settings on the local computer.

## 4.设置动作

IExecAction action = (IExecAction)task.Actions.Create(_TASK_ACTION_TYPE.TASK_ACTION_EXEC);
action.Path = "C:\\Windows\\System32\\calc.exe";

## 5.其他设置

task.Settings.ExecutionTimeLimit = "PT0S";
task.Settings.RunOnlyIfIdle = false;

### ExecutionTimeLimit属性

The format for this string is PnYnMnDTnHnMnS, where nY is the number of years, nM is the number of months, nD is the number of days, 'T' is the date/time separator, nH is the number of hours, nM is the number of minutes, and nS is the number of seconds (for example, PT5M specifies 5 minutes and P1M4DT2H5M specifies one month, four days, two hours, and five minutes). A value of PT0S will enable the task to run indefinitely.

## 6.注册任务

IRegisteredTask regTask = folder.RegisterTaskDefinition(
null, //user
"");

IRunningTask runTask = regTask.Run(null);

# 删除任务计划程序

private void DeleteTask(string taskName)
{
ts.Connect(null, null, null, null);
}

02-06
12-05 1860
06-09 4469
09-30 4446
03-25 112
09-29
10-13 85
02-21 153