C#查看、创建和删除系统任务计划程序
2013-01-22 13:29
429 查看
引言:这篇文章能够教你:
如何创建一个计划任务程序,每隔一段时间(最短1分钟)就执行一次特定的程序
注意下标是从1开始的。这样就遍历了所有的计划任务,可以依次查看它们的各个属性。如果你想创建什么样的计划任务,不妨先手动创建一个,再用这段代码查看,这样就知道该设置什么属性了
注:只有Connect之后才能使用
触发器有很多种,这个在手动创建计划任务的时候就会发现,如下图
当然最常用的就是按时间触发,每隔几天或每个几个月的触发器用IDailyTrigger,网上的很多都是以这个为例。我在这里就拿ITimerTrigger做个例子,能实现几分钟就触发一次的效果。
就是设置的循环时间,但并不是我们熟悉的毫秒数。它的值需要满足“PT1H1M”的格式,就是几小时几分钟执行一次,这个值对应的是触发器对话框的“重复任务间隔”如下图:
设置的值最终都会转换成分钟加入到触发器
官方解释:
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.
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.
也可以设置发一封邮件,不过还是执行程序的情况多
此属性的值对应的是“设置”选项卡的执行期限,如下图
如果想设置成如图所示的效果,就是把复选框勾掉,那就是PT0S;
官方解释:
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.
如下图
其中的BluceYoungTask就是此任务计划程序的名字
_TASK_LOGON_TYPE.TASK_LOGON_INTERACTIVE_TOKEN 的意思,貌似是用当前登录的账户去注册任务(所以登录名和密码都是null),而且只有在当前用户登录的情况下才起作用,其他的枚举值没有测试
官方说明参见:http://technet.microsoft.com/zh-cn/library/aa381365
如何创建一个计划任务程序,每隔一段时间(最短1分钟)就执行一次特定的程序
准备工作
复制C:\Windows\System32\taskschd.dll到项目下,添加引用,在属性窗口将“嵌入互操作类型”设置成false查看系统下所有的任务计划程序
TaskSchedulerClass ts = new TaskSchedulerClass(); ts.Connect(null, null, null, null); ITaskFolder folder = ts.GetFolder("\\"); IRegisteredTaskCollection tasks_exists = folder.GetTasks(1); for (int i = 1; i <= tasks_exists.Count; i++) { IRegisteredTask t = tasks_exists[i]; }
注意下标是从1开始的。这样就遍历了所有的计划任务,可以依次查看它们的各个属性。如果你想创建什么样的计划任务,不妨先手动创建一个,再用这段代码查看,这样就知道该设置什么属性了
创建自定义任务计划程序
1.实例化对象
//实例化任务对象 TaskSchedulerClass scheduler = new TaskSchedulerClass(); scheduler.Connect(null, null, null, null);//连接
注:只有Connect之后才能使用
2.设置基本属性
ITaskDefinition task = scheduler.NewTask(0); task.RegistrationInfo.Author = "BluceYoung";//创建者 task.RegistrationInfo.Description = "http://blog.csdn.net/bluceyoung";//描述
3.设置触发器
触发器有很多种,这个在手动创建计划任务的时候就会发现,如下图当然最常用的就是按时间触发,每隔几天或每个几个月的触发器用IDailyTrigger,网上的很多都是以这个为例。我在这里就拿ITimerTrigger做个例子,能实现几分钟就触发一次的效果。
ITimeTrigger tt = (ITimeTrigger)task.Triggers.Create(_TASK_TRIGGER_TYPE2.TASK_TRIGGER_TIME); tt.Repetition.Interval = "PT30M";//循环时间 tt.StartBoundary = "2013-01-21T14:27:25";//开始执行时间
Interval属性
就是设置的循环时间,但并不是我们熟悉的毫秒数。它的值需要满足“PT1H1M”的格式,就是几小时几分钟执行一次,这个值对应的是触发器对话框的“重复任务间隔”如下图:设置的值最终都会转换成分钟加入到触发器
官方解释:
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属性
开始执行时间,最常用的格式就是:2005-10-11T13:21:17。官方解释如下: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.DisallowStartIfOnBatteries = false; task.Settings.RunOnlyIfIdle = false;
ExecutionTimeLimit属性
此属性的值对应的是“设置”选项卡的执行期限,如下图如果想设置成如图所示的效果,就是把复选框勾掉,那就是PT0S;
官方解释:
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.
DisallowStartIfOnBatteries属性
对应的是“条件”选项卡的“只有在交流电源下才……”的复选框RunOnlyIfIdle属性
对应的是“条件”选项卡的“仅当计算机空闲时才……”的复选框如下图
6.注册任务
IRegisteredTask regTask = folder.RegisterTaskDefinition( "BluceYoungTask", task, (int)_TASK_CREATION.TASK_CREATE, null, //user null, // password _TASK_LOGON_TYPE.TASK_LOGON_INTERACTIVE_TOKEN, ""); IRunningTask runTask = regTask.Run(null);
其中的BluceYoungTask就是此任务计划程序的名字
_TASK_LOGON_TYPE.TASK_LOGON_INTERACTIVE_TOKEN 的意思,貌似是用当前登录的账户去注册任务(所以登录名和密码都是null),而且只有在当前用户登录的情况下才起作用,其他的枚举值没有测试
官方说明参见:http://technet.microsoft.com/zh-cn/library/aa381365
删除任务计划程序
private void DeleteTask(string taskName) { TaskSchedulerClass ts = new TaskSchedulerClass(); ts.Connect(null, null, null, null); ITaskFolder folder = ts.GetFolder("\\"); folder.DeleteTask(taskName, 0); }
相关文章推荐
- 实现记录和查看用户的系统登录和退出历史和利用C#编写的网络聊天程序
- oracle 创建,删除存储过程,参数传递,创建,删除存储函数,存储过程和函数的查看,包,系统包
- 自动创建计划任务之删除7天以前的备份文件批处理
- C# 双保险进程监视器 lol 保证被监视的程序"几乎"永远运行. 关键字:进程操作 进程查看 创建进程
- centOS 5 查看系统的版本和如何删除程序
- 使用系统自带的任务计划程序每天启动Emails服务器(Win7)
- oracle 创建,删除存储过程,参数传递,创建,删除存储函数,存储过程和函数的查看,包,系统包
- Asp.Net(C#)自动执行计划任务的程序实例分析分享
- C#读取注册表,C#创建任务计划
- 02.linux的文件系统简介、查看目录ls命令、切换目录cd命令、创建目录mkdir、删除目录rm、移动重命名mv、创建文件touch、拷贝cp
- C#程序中创建、复制、移动、删除文件或文件夹的示例
- Oracle下查看dbms_job、dbms_scheduler_job创建的任务计划的job
- pSOS&Win32--系统调用封装-任务管理(1): 创建 删除 取得
- 自动创建计划任务之删除7天以前的备份文件批处理
- C#查看程序占用系统的内存
- oracle 创建,删除存储过程,参数传递,创建,删除存储函数,存储过程和函数的查看,包,系统包
- C#:自动更新(从网络共享路径),程序删除,从网络复制,系统启动
- 牛客网Java刷题知识点之File对象常用功能:获取文件名称、获取文件路径、获取文件大小、获取文件修改时间、创建与删除、判断、重命名、查看系统根目录、容量获取、获取某个目录下内容、过滤器
- 使用 系统任务计划程序 实现定时自动关机
- 如何在windows server 2008 下创建系统自动任务来批量删除文件?[未完]