一:NuGet中搜索包:Microsoft.Win32.TaskScheduler并引用:
public static void RunTaskService(string vbsRootPath) { string taskName = "HelperTray1"; TaskService ts = new TaskService(); Microsoft.Win32.TaskScheduler.Task wsTask = ts.GetTask(taskName); if (wsTask == null) { string vbsStart = Path.Combine(vbsRootPath, "Start.vbs");//ApiConfig.ExtPath TaskDefinition td = ts.NewTask(); td.RegistrationInfo.Description = taskName; //开机后2分钟开始运行任务 //td.Triggers.Add(new BootTrigger { Delay = new TimeSpan(0, 2, 0) }); //参数1 Interval 间隔:每次重新启动任务之间的时间量。允许的最长时间为31天,允许的最短时间为1分钟 //参数2 Duration 持续时间:重复模式的持续时间。允许的最短时间是一分钟。如果指定了TimeSpan.Zero,则该模式将无限期地重复。 //参数3:获取或设置一个布尔值,该值指示正在运行的任务实例在重复模式持续时间结束时停止。 RepetitionPattern repetition = new RepetitionPattern(new TimeSpan(0, 1, 0), TimeSpan.Zero, true); td.Triggers.Add(new DailyTrigger { Repetition = repetition }); td.Actions.Add(new ExecAction("wscript.exe", vbsStart, null)); ts.RootFolder.RegisterTaskDefinition(taskName, td); } }
二、在系统C:\Windows\System32 下查找DLL:taskschd.dll并引用到项目中:
public static void CreateTaskScheduler(string vbsRootPath) { try { LogEvent.LogInfo.Fatal("2创建计划任务:1"); string taskName = "HelperTray2"; TaskSchedulerClass scheduler = new TaskSchedulerClass(); //pc-name/ip,username,domain,password scheduler.Connect(null, null, null, null); ITaskFolder folder = scheduler.GetFolder("\\"); IRegisteredTask rt = null; try { //搜索不到会抛异常 rt = folder.GetTask(taskName); if (rt != null) return; } catch(Exception ex) { LogEvent.LogInfo.Info("任务计划程序不存在,创建:"); } LogEvent.LogInfo.Fatal("2创建计划任务:2"); //set base attr ITaskDefinition task = scheduler.NewTask(0); //task.RegistrationInfo.Author = "system";//creator task.RegistrationInfo.Description = taskName; //set trigger (IDailyTrigger ITimeTrigger) 设置小时/分/秒来实现 // ITimeTrigger tt = (ITimeTrigger)task.Triggers.Create(_TASK_TRIGGER_TYPE2.TASK_TRIGGER_TIME); // tt.Repetition.Interval = interval;// format PT1H1M==1小时1分钟 设置的值最终都会转成分钟加入到触发器 //设置每天特定的时间来实现 IDailyTrigger tt = (IDailyTrigger)task.Triggers.Create(_TASK_TRIGGER_TYPE2.TASK_TRIGGER_DAILY); tt.StartBoundary = string.Format("1970-05-01T{0}:00:00", "04"); tt.EndBoundary = string.Format("2100-05-01T{0}:00:00", "04"); //tt.Repetition= new RepetitionPattern //start time //set action IExecAction action = (IExecAction)task.Actions.Create(_TASK_ACTION_TYPE.TASK_ACTION_EXEC); action.Path = "wscript.exe"; string vbsStart = Path.Combine(vbsRootPath, "HelperStart.vbs");//ApiConfig.ExtPath action.Arguments = vbsStart;//运行程序时需要的参数,如果没有可以不写。 task.Settings.ExecutionTimeLimit = "PT0S"; //运行任务时间超时停止任务吗? PTOS 不开启超时 task.Settings.DisallowStartIfOnBatteries = false;//只有在交流电源下才执行 task.Settings.RunOnlyIfIdle = false;//仅当计算机空闲下才执行 IRegisteredTask regTask = folder.RegisterTaskDefinition(taskName, task, (int)_TASK_CREATION.TASK_CREATE, null, null, _TASK_LOGON_TYPE.TASK_LOGON_INTERACTIVE_TOKEN, ""); IRunningTask runTask = regTask.Run(null); //return runTask.State; LogEvent.LogInfo.Fatal("2创建计划任务:3"); } catch (Exception ex) { LogEvent.LogInfo.Fatal("创建计划任务异常:"+ex); //throw ex; } }