Windows任务计划帮助类

基于 TaskScheduler 库操作Windows任务计划帮助类

简介

任务计划 可以将任何脚本、程序或文档安排在某个最方便的时间运行。常见于系统开机自启动程序,或定期运行自动更新程序或守护程序。

帮助类

TaskschdHelper

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/// <summary>
/// 创建任务计划
/// </summary>
/// <param name="strCreator">作者</param>
/// <param name="strTaskName">任务名称</param>
/// <param name="strPath">任务计划路径</param>
/// <param name="strInterval">任务触发时间(PT1M:1分钟,PT1H30M:90分钟)</param>
/// <param name="strStartBoundary">任务开始时间(yyyy-MM-ddTHH:mm:ss)</param>
/// <param name="strDescription">任务描述</param>
/// <returns>任务状态</returns>
public static bool CreateTaskschd(string strCreator, string strTaskName, string strPath, string strInterval, string strStartBoundary, string strDescription)
{
try
{
if (IsExists(strTaskName))
{
DeleteTaskschd(strTaskName);
}

//new scheduler
TaskSchedulerClass scheduler = new TaskSchedulerClass();
//pc-name/ip,username,domain,password
scheduler.Connect(null, null, null, null);
//get scheduler folder
ITaskFolder folder = scheduler.GetFolder("\\");

//set base attr
ITaskDefinition task = scheduler.NewTask(0);
task.RegistrationInfo.Author = strCreator;//creator
task.RegistrationInfo.Description = strDescription;//description

//set trigger (IDailyTrigger ITimeTrigger)
ITimeTrigger tt = (ITimeTrigger)task.Triggers.Create(_TASK_TRIGGER_TYPE2.TASK_TRIGGER_TIME);
tt.Repetition.Interval = strInterval;// format PT1H1M==1小时1分钟 设置的值最终都会转成分钟加入到触发器
tt.StartBoundary = strStartBoundary;//start time

//set action
IExecAction action = (IExecAction)task.Actions.Create(_TASK_ACTION_TYPE.TASK_ACTION_EXEC);
action.Path = strPath;//计划任务调用的程序路径

task.Settings.ExecutionTimeLimit = "PT0S"; //运行任务时间超时停止任务吗? PTOS 不开启超时
task.Settings.DisallowStartIfOnBatteries = false;//只有在交流电源下才执行
task.Settings.RunOnlyIfIdle = false;//仅当计算机空闲下才执行

IRegisteredTask regTask = folder.RegisterTaskDefinition(strTaskName, task,
(int)_TASK_CREATION.TASK_CREATE, null, //user
null, //password
_TASK_LOGON_TYPE.TASK_LOGON_INTERACTIVE_TOKEN,
"");
IRunningTask runTask = regTask.Run(null);
//return runTask.State;
return true;
}
catch (Exception ex)
{
TXTHelper.Logs(ex.ToString());
//return _TASK_STATE.TASK_STATE_UNKNOWN;
return false;
}
}

/// <summary>
/// 删除任务计划
/// </summary>
/// <param name="strTaskName">任务计划名称</param>
/// <returns>成功返回true,失败返回false</returns>
public static bool DeleteTaskschd(string strTaskName)
{
try
{
TaskSchedulerClass taskScheduler = new TaskSchedulerClass();
taskScheduler.Connect(null, null, null, null);
ITaskFolder taskFolder = taskScheduler.GetFolder("\\");
taskFolder.DeleteTask(strTaskName, 0);
return true;
}
catch (Exception ex)
{
TXTHelper.Logs(ex.ToString());
return false;
}
}

/// <summary>
/// 获得所有任务计划
/// </summary>
/// <returns>所有任务计划</returns>
public static IRegisteredTaskCollection GetAllTaskschd()
{
try
{
TaskSchedulerClass taskScheduler = new TaskSchedulerClass();
taskScheduler.Connect(null, null, null, null);
ITaskFolder taskFolder = taskScheduler.GetFolder("\\");
IRegisteredTaskCollection tasks_exists = taskFolder.GetTasks(1);
return tasks_exists;
}
catch (Exception ex)
{
TXTHelper.Logs(ex.ToString());
return null;
}
}

/// <summary>
/// 任务计划是否存在
/// </summary>
/// <param name="strTaskName">任务计划名称</param>
/// <returns></returns>
public static bool IsExists(string strTaskName)
{
try
{
bool isExists = false;
IRegisteredTaskCollection tasks_exists = GetAllTaskschd();
for (int i = 1; i <= tasks_exists.Count; i++)
{
IRegisteredTask registeredTask = tasks_exists[i];
if (registeredTask.Name.Equals(strTaskName))
{
isExists = true;
break;
}
}
return isExists;
}
catch (Exception ex)
{
TXTHelper.Logs(ex.ToString());
return false;
}
}