public void removetask(string name) { using (Microsoft.Win32.TaskScheduler.TaskService ts = new Microsoft.Win32.TaskScheduler.TaskService()) { ts.RootFolder.DeleteTask(name); } }
/// <summary> /// Updates a task, or creates it if not existing /// </summary> /// <param name="aName">Task Name</param> /// <param name="aTask">Action to run</param> /// <param name="aDescription">Plain text</param> /// <param name="aUserName">Domain/User</param> /// <param name="aPassword">Secure password</param> /// <param name="aTrigger">Trigger to use</param> /// <param name="aArgument">Arguments to Action</param> public static void CreateOrUpdateTask(string aName, string aTask, string aDescription, string aUserName, System.Security.SecureString aPassword, Microsoft.Win32.TaskScheduler.Trigger aTrigger, string aArgument) { // Get the service on the local machine using (Microsoft.Win32.TaskScheduler.TaskService ts = new Microsoft.Win32.TaskScheduler.TaskService()) { // Create a new task definition and assign properties Microsoft.Win32.TaskScheduler.TaskDefinition td = ts.NewTask(); td.RegistrationInfo.Description = aDescription; td.RegistrationInfo.Author = "XervBackup Scheduler"; // Create a trigger that will fire the task td.Triggers.Add(aTrigger); td.Settings.WakeToRun = true; // Create an action that will launch the task whenever the trigger fires td.Actions.Add(new Microsoft.Win32.TaskScheduler.ExecAction(aTask, aArgument, null)); if (ts.HighestSupportedVersion > new Version(1, 2)) { td.Principal.LogonType = Microsoft.Win32.TaskScheduler.TaskLogonType.Password; td.Principal.RunLevel = Microsoft.Win32.TaskScheduler.TaskRunLevel.LUA; } // Register the task in the root folder string contents = null; if (aPassword != null) { IntPtr ptr = System.Runtime.InteropServices.Marshal.SecureStringToBSTR(aPassword); contents = System.Runtime.InteropServices.Marshal.PtrToStringAuto(ptr); } ts.RootFolder.RegisterTaskDefinition(aName, td, Microsoft.Win32.TaskScheduler.TaskCreation.CreateOrUpdate, aUserName, contents, Microsoft.Win32.TaskScheduler.TaskLogonType.Password, null); } }
private void RemoveSystemSchedule(Schedule schedule) { using (Microsoft.Win32.TaskScheduler.TaskService ts = new Microsoft.Win32.TaskScheduler.TaskService()) { ts.RootFolder.DeleteTask(schedule.Name); } }
private void SetSystemSchedule(Schedule schedule) { using (Microsoft.Win32.TaskScheduler.TaskService ts = new Microsoft.Win32.TaskScheduler.TaskService()) { Microsoft.Win32.TaskScheduler.TaskDefinition td = ts.NewTask(); td.RegistrationInfo.Description = schedule.Description; td.Triggers.Add(GetTrigger(schedule)); td.Actions.Add(GetAction(schedule)); ts.RootFolder.RegisterTaskDefinition(schedule.Name, td); } }
/// <summary> /// Add a startup task (not used) /// </summary> /// <param name="aName">Task Name</param> /// <param name="aTask">Action to run</param> /// <param name="aDescription">Plain text</param> /// <param name="aUserName">Domain/User</param> /// <param name="aPassword">Secure password</param> public static void AddStartup(string aName, string aTask, string aDescription, string aUserName, System.Security.SecureString aPassword) { // Get the service on the local machine using (Microsoft.Win32.TaskScheduler.TaskService ts = new Microsoft.Win32.TaskScheduler.TaskService()) { // Create a new task definition and assign properties Microsoft.Win32.TaskScheduler.TaskDefinition td = ts.NewTask(); td.RegistrationInfo.Description = aDescription; // Create a trigger that will fire the task at boot td.Triggers.Add(new Microsoft.Win32.TaskScheduler.BootTrigger()); // Create an action that will launch the task whenever the trigger fires td.Actions.Add(new Microsoft.Win32.TaskScheduler.ExecAction(aTask, null, null)); td.Principal.LogonType = Microsoft.Win32.TaskScheduler.TaskLogonType.Password; td.Principal.RunLevel = Microsoft.Win32.TaskScheduler.TaskRunLevel.Highest; // Register the task in the root folder ts.RootFolder.RegisterTaskDefinition(aName, td, Microsoft.Win32.TaskScheduler.TaskCreation.Create, aUserName, System.Runtime.InteropServices.Marshal.PtrToStringAuto(System.Runtime.InteropServices.Marshal.SecureStringToBSTR(aPassword)), Microsoft.Win32.TaskScheduler.TaskLogonType.Password, null); } }
/// <summary> /// Set a trigger /// </summary> /// <param name="aName">Task Name</param> /// <param name="aTrigger">Trigger to set</param> /// <returns>True if all OK</returns> public static bool SetTrigger(string aName, Microsoft.Win32.TaskScheduler.Trigger aTrigger) { bool Result = false; using (Microsoft.Win32.TaskScheduler.TaskService ts = new Microsoft.Win32.TaskScheduler.TaskService()) using (Microsoft.Win32.TaskScheduler.Task Task = ts.GetTask(aName)) { if (Task != null) { if (Task.Definition.Triggers.Count > 0) Task.Definition.Triggers[0] = aTrigger; // We only use one trigger... else Task.Definition.Triggers.Add(aTrigger); Result = true; } } return Result; }
/// <summary> /// Returns a trigger given Task Name /// </summary> /// <param name="aName">Task Name</param> /// <returns>Trigger or null</returns> public static Microsoft.Win32.TaskScheduler.Trigger GetTrigger(string aName) { Microsoft.Win32.TaskScheduler.Trigger Result = null; using (Microsoft.Win32.TaskScheduler.TaskService ts = new Microsoft.Win32.TaskScheduler.TaskService()) using( Microsoft.Win32.TaskScheduler.Task Task = ts.GetTask(aName) ) { if (Task != null) { if (Task.Definition.Triggers.Count > 0) Result = Task.Definition.Triggers[0]; // We only use one trigger... } } return Result; }
/// <summary> /// Does a taskname exist? /// </summary> /// <param name="aName">Task Name</param> /// <returns>True if existing</returns> public static bool Exists(string aName) { bool Result = false; // Get the service on the local machine using (Microsoft.Win32.TaskScheduler.TaskService ts = new Microsoft.Win32.TaskScheduler.TaskService()) Result = ts.GetTask(aName) != null; return Result; }
public void createtask(string name, string description, string path, string parameters, string workingdirectory, short day) { using (Microsoft.Win32.TaskScheduler.TaskService ts = new Microsoft.Win32.TaskScheduler.TaskService()) { Microsoft.Win32.TaskScheduler.TaskDefinition td = ts.NewTask(); td.RegistrationInfo.Description = description; td.Triggers.Add(new Microsoft.Win32.TaskScheduler.DailyTrigger { DaysInterval = day }); td.Actions.Add(new Microsoft.Win32.TaskScheduler.ExecAction(path, parameters, workingdirectory)); ts.RootFolder.RegisterTaskDefinition(@"" + name, td); } }
private void btnSaveProfile_Click(object sender, EventArgs e) { if (cbProfiles.Text == "") { MessageBox.Show("Please enter a profile name before attempting to save the profile."); cbProfiles.Focus(); return; } if (tbECID.Text.Length == 0) { MessageBox.Show("You must enter a device ECID before attempting to save the profile."); tbECID.Focus(); return; } if (chkSchedulingEnabled.Checked) { using (Microsoft.Win32.TaskScheduler.TaskService ts = new Microsoft.Win32.TaskScheduler.TaskService()) { Microsoft.Win32.TaskScheduler.Task t = ts.GetTask(frmAutosaves.taskName); if (t == null) { MessageBox.Show("You selected to automatically check and save blobs for this profile, but you haven't set up autochecking. If you would like to use the autocheck feature, please enable it now."); frmAutosaves formAutosaves = new frmAutosaves(); formAutosaves.ShowDialog(); } } } try { string identifier = ""; if (cbModel.Text.Length > 0) { identifier = cbModel.Text.Substring(cbModel.Text.IndexOf("(") + 1); identifier = identifier.Substring(0, identifier.LastIndexOf(")")); } StringBuilder sb = new StringBuilder(); sb.AppendLine("SchedulingEnabled:" + chkSchedulingEnabled.Checked); sb.AppendLine("ECID:" + tbECID.Text); sb.AppendLine("DeviceType:" + cbDevice.Text); sb.AppendLine("Identifier:" + identifier); sb.AppendLine("InternalName:" + cbBoardConfig.Text); sb.AppendLine("Apnonce:" + cbApnonce.Text); sb.Append("ApnonceList:"); for (int i = 0; i < cbApnonce.Items.Count; i++) { string apnonce = cbApnonce.GetItemText(cbApnonce.Items[i]); if (apnonce.Length == 0) { continue; } sb.Append(";" + apnonce); // Preceeding the apnonce with a semicolon will handle adding the first blank item automatically } File.WriteAllText(Path.Combine(pathProfiles, cbProfiles.Text + ".txt"), sb.ToString()); PopupNotifier popup = PopupHelper.GeneratePopup("The profile was saved successfully."); popup.Popup(); } catch (Exception ex) { MessageBox.Show("Error: " + ex.Message + "\n\n" + ex.StackTrace); } RefreshProfiles(); }