需要引用Microsoft.Win32.TaskScheduler.dll 类库 记得using Microsoft.Win32.TaskScheduler; ////// 创建windows计划任务/// /// 任务名称/// 执行时间public static void CreateTask(string taskName, DateTime stateTime){try{// 计划任务服务TaskService ts = new TaskService();// 创建一个计划任务TaskDefinition td = ts.NewTask();td.RegistrationInfo.Description = "DTU执行未执行过的任务"; //任务描述td.RegistrationInfo.Author = "EcpDtu"; //任务作者//时间执行时间TimeTrigger tt = new TimeTrigger();tt.StartBoundary = DateTime.Now.AddSeconds(5);//stateTime.AddHours(4); //开始时间tt.EndBoundary = DateTime.Now.AddSeconds(10);//stateTime.AddHours(8); //结束执行时间td.Triggers.Add(tt); //将触发器添加到任务中//创建一个执行操作var exe = new ExecAction("RunTask.exe", null, ConfigurationManager.AppSettings["RunTask_path"]);//添加执行操作到计划任务的操作中 td.Actions.Add(exe);//注册计划任务 ts.RootFolder.RegisterTaskDefinition(taskName, td);}catch (Exception ex){MsgHelper.ErrorWriteLog("创建计划任务错误_CreateTask(string name)" + ex.Message);}}/// /// 查询计划任务/// /// public static void RetrieveTestTask(string taskName){// 计划任务服务TaskService ts = new TaskService();Task t = ts.GetTask(taskName);if (t != null){Console.WriteLine("Task Name={0}", t.Name);Console.WriteLine("Task Execution Time={0}", t.LastRunTime);Console.WriteLine("Task Last Run Result={0}", t.LastTaskResult);Console.WriteLine("Task Next Execution Time={0}", t.NextRunTime);}}/// /// 删除计划任务/// public static void DeleteTestTask(string taskName){// 计划任务服务TaskService ts = new TaskService();ts.RootFolder.DeleteTask(taskName);}