一: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; } }