Skip to content

Instantly share code, notes, and snippets.

@sphingu
Created June 14, 2013 11:56
Show Gist options
  • Save sphingu/5781276 to your computer and use it in GitHub Desktop.
Save sphingu/5781276 to your computer and use it in GitHub Desktop.
Add Schedular to TaskSchedular in C#
using Microsoft.Win32.TaskScheduler;
private void AddTask()
{
// Get the service on the local machine
using (TaskService ts = new TaskService())
{
// Create a new task definition and assign properties
TaskDefinition td = ts.NewTask();
td.RegistrationInfo.Description = "Tesing Schedule";
// Create a trigger that will fire the task at this time every other day
td.Triggers.Add(new DailyTrigger { DaysInterval = 1, StartBoundary = DateTime.Now.Subtract(DateTime.Now.TimeOfDay).AddHours(19) });
// Create an action that will launch Notepad whenever the trigger fires
td.Actions.Add((new ExecAction("C:\\Mail\\EntryReport.exe", null, null)));
// Register the task in the root folder
ts.RootFolder.RegisterTaskDefinition(@"EntryReportScheduler", td);
// Remove the task we just created
ts.RootFolder.DeleteTask("Test");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment