标签:
首先需要引用system32下的taskschd.dll
测试环境:win8+vs2012+.net4.0
1 /// <summary> 2 /// 删除任务 3 /// </summary> 4 /// <param name="taskName"></param> 5 private static void DeleteTask(string taskName) 6 { 7 TaskSchedulerClass ts = new TaskScheduler.TaskSchedulerClass(); 8 ts.Connect(null, null, null, null); 9 ITaskFolder folder = ts.GetFolder("\\"); 10 folder.DeleteTask(taskName, 0); 11 } 12 13 /// <summary> 14 /// 获取所有任务 15 /// </summary> 16 public static IRegisteredTaskCollection GetAllTasks() 17 { 18 TaskSchedulerClass ts = new TaskSchedulerClass(); 19 ts.Connect(null, null, null, null); 20 ITaskFolder folder = ts.GetFolder("\\"); 21 IRegisteredTaskCollection tasks_exists = folder.GetTasks(1); 22 return tasks_exists; 23 } 24 25 /// <summary> 26 /// 任务是否存在 27 /// </summary> 28 /// <param name="taskName"></param> 29 /// <returns></returns> 30 public static bool IsExists(string taskName) 31 { 32 var isExists = false; 33 IRegisteredTaskCollection tasks_exists = GetAllTasks(); 34 for (int i = 1; i <= tasks_exists.Count; i++) 35 { 36 IRegisteredTask t = tasks_exists[i]; 37 if (t.Name.Equals(taskName)) 38 { 39 isExists = true; 40 break; 41 } 42 } 43 return isExists; 44 } 45 46 /// <summary> 47 /// 创建任务 48 /// </summary> 49 /// <param name="creator"></param> 50 /// <param name="taskName"></param> 51 /// <param name="path"></param> 52 /// <param name="starttime"></param> 53 /// <param name="interval"></param> 54 /// <param name="description"></param> 55 /// <returns></returns> 56 public static _TASK_STATE CreateTaskScheduler(string creator, string taskName, string path, string starttime, string interval, string description) 57 { 58 try 59 { 60 if (IsExists(taskName)) 61 { 62 DeleteTask(taskName); 63 } 64 65 //new scheduler 66 TaskSchedulerClass scheduler = new TaskSchedulerClass(); 67 //pc-name/ip,username,domain,password 68 scheduler.Connect(null, null, null, null); 69 //get scheduler folder 70 ITaskFolder folder = scheduler.GetFolder("\\"); 71 72 //set base attr 73 ITaskDefinition task = scheduler.NewTask(0); 74 task.RegistrationInfo.Author = creator;//creator 75 task.RegistrationInfo.Description = description; 76 77 //set trigger (IDailyTrigger ITimeTrigger) 78 ITimeTrigger tt = (ITimeTrigger)task.Triggers.Create(_TASK_TRIGGER_TYPE2.TASK_TRIGGER_TIME); 79 tt.Repetition.Interval = interval;// format PT1H1M==1小时1分钟 设置的值最终都会转成分钟加入到触发器 80 tt.StartBoundary = starttime;//start time 81 82 //set action 83 IExecAction action = (IExecAction)task.Actions.Create(_TASK_ACTION_TYPE.TASK_ACTION_EXEC); 84 action.Path = path; 85 action.Arguments = "AB1";//运行程序时需要的参数,如果没有可以不写。 86 87 task.Settings.ExecutionTimeLimit = "PT0S"; //运行任务时间超时停止任务吗? PTOS 不开启超时 88 task.Settings.DisallowStartIfOnBatteries = false;//只有在交流电源下才执行 89 task.Settings.RunOnlyIfIdle = false;//仅当计算机空闲下才执行 90 91 IRegisteredTask regTask = folder.RegisterTaskDefinition(taskName, task, 92 (int)_TASK_CREATION.TASK_CREATE, 93 null, //user 94 null, // password 95 _TASK_LOGON_TYPE.TASK_LOGON_INTERACTIVE_TOKEN, 96 ""); 97 IRunningTask runTask = regTask.Run(null); 98 return runTask.State; 99 100 } 101 catch (Exception ex) 102 { 103 throw ex; 104 } 105 106 }
调用方法:
1 private void button5_Click(object sender, EventArgs e) 2 { 3 //string a = WindowsIdentity.GetCurrent().Name; 4 //string b = Environment.UserName; 5 //string c = System.Net.Dns.GetHostName(); 6 7 string creator = "Hello"; 8 string taskName = "Hello.Task"; 9 string path = Application.StartupPath + "\\aaaa.bat"; 10 string interval = "PT1H1M";//格式 PT1H1M==1小时1分钟 11 string starttime = DateTime.Now.ToString("yyyy-MM-ddT00:00:00"); //格式 "2015-10-09T14:27:25"; 12 string description = ""; 13 14 CreateTaskScheduler(creator, taskName, path, starttime, interval, description); 15 }
标签:
原文地址:http://www.cnblogs.com/wz122889488/p/4915165.html