Project Description
This project provides a single assembly wrapper for the 1.0 and 2.0 versions of Task Scheduler found in all Microsoft operating systems post Windows 98. It simplifies the coding, aggregates the multiple versions, provides an editor and allows for localization support.

Microsoft introduced version 2.0 (internally version 1.2) with a completely new object model with Windows Vista. The managed assembly closely resembles the new object model, but allows the 1.0 (internally version 1.1) COM objects to be manipulated. It will automatically choose the most recent version of the library found on the host system (up through 1.4). The project was written in C#, but works with any .NET language including scripting languages.

The project is based on work the originator started in January 2002 with the 1.0 library that is currently hosted on CodeProject.

There is a second library that includes a GUI editor and a wizard for tasks which mimic the ones in Vista and later and adds optional pages for new properties. There are also a few UI goodies for you code junkies. There is a DropDownCheckList control that is very useful for selecting flag type enumerations. There is a FullDateTimePicker control which allows both date and time selection in a single control. There is a CredentialsDialog class for prompting for a password which wraps the Windows API. Lastly, there is code that simplifies pulling events from the system event log.

There is a help file included with the download that provides an overview of the various classes. Below is a brief example of how to use the library from C#.

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
         ts.RootFolder.DeleteTask("Test");
      }
   }
}
If you really want to squeeze things into a single line of code (without any error handling):

new TaskService().AddTask("Test", new DailyTrigger { DaysInterval = 2 }, new ExecAction("notepad.exe", "c:\\test.log", null));
For extended examples on how to the use the library, look in the source code area or look at the Examples Page. The library closely follows the Task Scheduler 2.0 Scripting classes. Microsoft has some examples on MSDN around it that may further help you understand how to use this library.

Last edited Oct 29, 2012 at 2:51 PM by dahall, version 19