Documentation Home Page
There is a help file included with the download that provides an overview of the various classes. The Microsoft MSDN documentation provides an excellent overview of the Task Scheduler along with details around security and permission, idle conditions, and trigger repetition.- The Classes Overview describes each of the major classes and how they are used.
- The Examples Page shows some C# code that demonstrates almost every function of the library.
- Trigger Example Code has specific examples around triggers.
- Action Example Code has specific examples around actions.
- Task Event Management Code has examples around how to view and watch events related to tasks.
- Security Explanations and Samples show how to connect to remote servers and how to create tasks that run in different security contexts (system account, user account, elevated, etc.)
- PowerShell Examples has examples on how to use the library from PowerShell.
- The Installation & Samples Page explains how to use this library in your own projects and includes a sample project.
using System; using Microsoft.Win32.TaskScheduler; class Program { staticvoid Main(string[] args) { // Get the service on the local machineusing (TaskService ts = new TaskService()) { // Create a new task definition and assign properties TaskDefinition td = ts.NewTask(); td.RegistrationInfo.Description = "Does something"; // Add a trigger that, starting tomorrow, will fire every other week on Monday// and Saturday and repeat every 10 minutes for the following 11 hours WeeklyTrigger wt = new WeeklyTrigger(); wt.StartBoundary = DateTime.Today.AddDays(1); wt.DaysOfWeek = DaysOfTheWeek.Monday | DaysOfTheWeek.Saturday; wt.WeeksInterval = 2; wt.Repetition.Duration = TimeSpan.FromHours(11); wt.Repetition.Interval = TimeSpan.FromMinutes(10); td.Triggers.Add(wt) // 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); } } }
Here's the same as above but in VB.NET
Imports Microsoft.Win32.TaskScheduler Module Module1 Sub Main() Using ts AsNew TaskService() ' Create a new task definition and assign propertiesDim td As TaskDefinition = ts.NewTask td.RegistrationInfo.Description = "Does something"' Add a trigger that will, starting tomorrow, fire every other week on Monday' and Saturday and repeat every 10 minutes for the following 11 hoursDim wt AsNew WeeklyTrigger() wt.StartBoundary = DateTime.Today.AddDays(1) wt.DaysOfWeek = DaysOfTheWeek.Monday Or DaysOfTheWeek.Saturday wt.WeeksInterval = 2 wt.Repetition.Duration = TimeSpan.FromHours(11) wt.Repetition.Interval = TimeSpan.FromMinutes(10) td.Triggers.Add(wt) ' Add an action (shorthand) that runs Notepad td.Actions.Add(New ExecAction("notepad.exe", "c:\test.log")) ' Register the task in the root folder ts.RootFolder.RegisterTaskDefinition("Test", td) EndUsingEndSubEndModule
If you're really into shorthand code, here's almost the same functionality as the C# code above, but much shorter:
using System; using Microsoft.Win32.TaskScheduler; class Program { staticvoid Main(string[] args) { new TaskService().AddTask("Test", new DailyTrigger { DaysInterval = 2 }, new ExecAction("notepad.exe", "c:\\test.log", null)); } }
Alternately, you can use the library declaratively or "fluently":
Task t = new TaskServer().Execute("notepad.exe").WithArguments(@"c:\test.log").Every(2).Days().AsTask("Test");