private void Prox_Ejec() { string nombreApp = (Path.GetFileName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)).Replace("Config.exe", ""); ScheduledTasks Tareas = new ScheduledTasks(); TaskScheduler.Task tarea = Tareas.OpenTask(nombreApp); if (chkInicioW.Checked && tarea != null) { string proxima = tarea.NextRunTime.ToString(); label18.Visible = true; label18.Text = ("Proxima ejecucion: " + proxima); chkEdit.Visible = true; TimePicker.Value = Convert.ToDateTime(proxima); tarea.Close(); Dehab(); } else { label18.Visible = false; label18.Text = ""; chkEdit.Visible = false; chkInicioW.Checked = false; } chkEdit.Checked = false; Tareas.Dispose(); }
/// <summary> /// Creates a new task on the system with the given <paramref name="name" />. /// </summary> /// <remarks>Task names follow normal filename character restrictions. The name /// will be come the name of the file used to store the task (with .job added).</remarks> /// <param name="name">Name for the new task.</param> /// <returns>Instance of new task.</returns> /// <exception cref="ArgumentException">There is an existing task with the given name.</exception> public Task CreateTask(string name) { Task tester = OpenTask(name); if (tester != null) { tester.Close(); throw new ArgumentException("The task \"" + name + "\" already exists."); } try { object o; its.NewWorkItem(name, ref CTaskGuid, ref ITaskGuid, out o); ITask iTask = (ITask)o; return(new Task(iTask, name)); } catch { return(null); } }
private bool IniciarWindows() { string nombreApp = (Path.GetFileName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)).Replace("Config.exe", ""); ScheduledTasks Tareas = new ScheduledTasks(); string[] Existen = Tareas.GetTaskNames(); short hora = Convert.ToInt16(TimePicker.Text.Substring(0, 2)); short min = Convert.ToInt16(TimePicker.Text.Substring(3, 2)); string pass = null; if (chkInicioW.Checked == true) { try { //Referencia https://www.codeproject.com/Articles/2407/A-New-Task-Scheduler-Class-Library-for-NET if (!Existen.Contains(nombreApp + ".job")) { TaskScheduler.Task tarea = Tareas.CreateTask(nombreApp); tarea.ApplicationName = txtRutaRead.Text; tarea.Comment = "Tarea para ejecutar el ReadFiles diariamente"; tarea.SetAccountInformation(Environment.UserName, pass); tarea.Creator = Environment.UserName; tarea.Priority = System.Diagnostics.ProcessPriorityClass.Normal; tarea.Triggers.Add(new DailyTrigger(hora, min)); tarea.Flags = TaskFlags.RunOnlyIfLoggedOn; tarea.Save(); tarea.Close(); } else if (chkEdit.Checked) { TaskScheduler.Task tarea = Tareas.OpenTask(nombreApp); tarea.Triggers.RemoveAt(0); tarea.Triggers.Add(new DailyTrigger(hora, min)); tarea.Save(); tarea.Close(); } Tareas.Dispose(); Prox_Ejec(); txtPassWind.Text = ""; return(true); } catch (Exception e) { Tareas.DeleteTask(nombreApp); Tareas.Dispose(); MessageBox.Show(e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); Prox_Ejec(); return(false); } } else { try { Tareas.DeleteTask(nombreApp); Tareas.Dispose(); Prox_Ejec(); return(true); } catch (Exception e) { Prox_Ejec(); return(false); } } }