Boardy Boardy - 10 months ago 77
C# Question

Creating Scheduled Tasks

I am currently working on a C# WPF project. I need to allow the user to create and add a scheduled task to the Windows Task Scheduler.

How could I go about doing this and what using directives and references do I need as I am not finding much when searching the Internet.

Answer Source

You can use Task Scheduler Managed Wrapper:

using System;
using Microsoft.Win32.TaskScheduler;

class Program
   static void Main(string[] args)
      // 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 = "Does something";

         // Create a trigger that will fire the task at this time every other day
         td.Triggers.Add(new DailyTrigger { DaysInterval = 2 });

         // Create an action that will launch Notepad whenever the trigger fires
         td.Actions.Add(new ExecAction("notepad.exe", "c:\\test.log", null));

         // Register the task in the root folder
         ts.RootFolder.RegisterTaskDefinition(@"Test", td);

         // Remove the task we just created

Alternatively you can use native API or go for Quartz.NET. See this for details.