1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
public static bool CreateTaskschd(string strCreator, string strTaskName, string strPath, string strInterval, string strStartBoundary, string strDescription) { try { if (IsExists(strTaskName)) { DeleteTaskschd(strTaskName); }
TaskSchedulerClass scheduler = new TaskSchedulerClass(); scheduler.Connect(null, null, null, null); ITaskFolder folder = scheduler.GetFolder("\\");
ITaskDefinition task = scheduler.NewTask(0); task.RegistrationInfo.Author = strCreator; task.RegistrationInfo.Description = strDescription;
ITimeTrigger tt = (ITimeTrigger)task.Triggers.Create(_TASK_TRIGGER_TYPE2.TASK_TRIGGER_TIME); tt.Repetition.Interval = strInterval; tt.StartBoundary = strStartBoundary;
IExecAction action = (IExecAction)task.Actions.Create(_TASK_ACTION_TYPE.TASK_ACTION_EXEC); action.Path = strPath;
task.Settings.ExecutionTimeLimit = "PT0S"; task.Settings.DisallowStartIfOnBatteries = false; task.Settings.RunOnlyIfIdle = false;
IRegisteredTask regTask = folder.RegisterTaskDefinition(strTaskName, task, (int)_TASK_CREATION.TASK_CREATE, null, null, _TASK_LOGON_TYPE.TASK_LOGON_INTERACTIVE_TOKEN, ""); IRunningTask runTask = regTask.Run(null); return true; } catch (Exception ex) { TXTHelper.Logs(ex.ToString()); return false; } }
public static bool DeleteTaskschd(string strTaskName) { try { TaskSchedulerClass taskScheduler = new TaskSchedulerClass(); taskScheduler.Connect(null, null, null, null); ITaskFolder taskFolder = taskScheduler.GetFolder("\\"); taskFolder.DeleteTask(strTaskName, 0); return true; } catch (Exception ex) { TXTHelper.Logs(ex.ToString()); return false; } }
public static IRegisteredTaskCollection GetAllTaskschd() { try { TaskSchedulerClass taskScheduler = new TaskSchedulerClass(); taskScheduler.Connect(null, null, null, null); ITaskFolder taskFolder = taskScheduler.GetFolder("\\"); IRegisteredTaskCollection tasks_exists = taskFolder.GetTasks(1); return tasks_exists; } catch (Exception ex) { TXTHelper.Logs(ex.ToString()); return null; } }
public static bool IsExists(string strTaskName) { try { bool isExists = false; IRegisteredTaskCollection tasks_exists = GetAllTaskschd(); for (int i = 1; i <= tasks_exists.Count; i++) { IRegisteredTask registeredTask = tasks_exists[i]; if (registeredTask.Name.Equals(strTaskName)) { isExists = true; break; } } return isExists; } catch (Exception ex) { TXTHelper.Logs(ex.ToString()); return false; } }
|