Task Scheduler Managed Wrapper

repository·master·Indexed 23 days ago

https://github.com/dahall/taskscheduler

A .NET library providing a managed interface for the Windows Task Scheduler, aggregating multiple API versions for compatibility across legacy (XP/V1) and modern (Vista+/V2) systems. It includes the main TaskScheduler library for task creation and management via the Microsoft.Win32.TaskScheduler namespace, and the TaskSchedulerEditor library providing localized GUI editors, wizards, and dialogs that mimic Windows system editors.

Tokens
2.6K
Snippets
6
Records
9
Agent score
79%

What's inside TaskScheduler

  1. Overview of TaskScheduler Project Components

    master

    The project consists of two primary libraries:

    Main Library (TaskScheduler)

    A managed wrapper for the Windows Task Scheduler that works across different Windows versions (including V1/XP and V2/Vista+). Key features include:

    • Cross-version support: Automatically selects the most recent library version on the host system.
    • Enhanced V1 support: Supports multiple actions and all action types (via PowerShell) on older systems like XP/WS2003.
    • Advanced Task Creation: Supports fluent methods, Cron syntax for triggers, and serialization to XML.
    • Compatibility: Works with any .NET language (C#, PowerShell, etc.) and supports .NET Standard 2.0, .NET Core, and various .NET Framework versions.

    UI Library (TaskSchedulerEditor)

    Provides localized GUI editors and wizards that mimic the Windows system editors. Available controls include:

    • TaskEditDialog and TaskOptionsEditor (Task editors)
    • Task creation wizards
    • Action, Trigger, and Event viewer dialogs
    • Task/folder selection dialogs
    • CredentialsDialog for Windows API password prompting
  2. Use the TaskEditor UI library

    master

    The TaskEditor library is a Windows Forms User Interface library designed to work with the TaskScheduler .NET wrapper. It provides UI controls that mimic the Windows Task Scheduler application, allowing users to manage tasks through familiar dialogs and wizards.

    Key UI components include:

    • Task Editors: TaskEditDialog and TaskPropertiesControl (mimics system editor), or TaskOptionsEditor (modern UI scheme).
    • Wizards: TaskSchedulerWizard for task creation.
    • Specific Editors: ActionEditDialog for actions, TriggerEditDialog for triggers, and CredentialsDialog for password prompting.
    • Browsers & Viewers: TaskBrowserDialog for selecting tasks/folders, EventViewerDialog/EventViewerControl for Windows Event Logs, and TaskHistoryControl for viewing task history.
    • Specialized Controls: DropDownCheckList for flag enumerations and FullDateTimePicker for combined date/time selection.
    // Create a new task
    Task t = TaskService.Instance.AddTask("Test", QuickTriggerType.Daily, "myprogram.exe");
    
    // Edit task and re-register if user clicks Ok
    TaskEditDialog editorForm = new TaskEditDialog(task: t, editable: true, registerOnAccept: true);
    editorForm.ShowDialog();
  3. Install TaskScheduler via NuGet

    master

    You can install the main library using NuGet. The assemblies are available under the package name TaskScheduler. Once referenced in your project, all classes are located in the Microsoft.Win32.TaskScheduler namespace.

    For UI components, install the TaskSchedulerEditor package.

  4. Create complex tasks with TaskDefinition

    master

    For advanced configurations, use the TaskService object to create a TaskDefinition. This allows you to explicitly define registration information, multiple triggers (such as DailyTrigger), and multiple actions (such as ExecAction).

    To register the task, use ts.RootFolder.RegisterTaskDefinition. When working with remote machines, ensure you provide the appropriate credentials during TaskService initialization and during task registration to ensure proper permissions.

    using System;
    using Microsoft.Win32.TaskScheduler;
    
    class Program
    {
       static void Main()
       {
          // Get the service on the remote machine
          using (TaskService ts = new TaskService(@"\\RemoteServer", "username", "domain", "password"))
          {
             // 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.
             // (Use the username here to ensure remote registration works.)
             ts.RootFolder.RegisterTaskDefinition(@"Test", td, TaskCreation.CreateOrUpdate, "username");
          }
       }
    }
  5. Edit a task using TaskEditDialog

    master

    To allow a user to edit an existing task and automatically save changes back to the system, use the TaskEditDialog.

    When initializing the dialog, set editable: true to allow modifications and registerOnAccept: true to ensure that clicking 'OK' in the dialog automatically re-registers the task with the Windows Task Scheduler service.

    // Edit task and re-register if user clicks Ok
    TaskEditDialog editorForm = new TaskEditDialog(task: t, editable: true, registerOnAccept: true);
    editorForm.ShowDialog();
  6. Create simple tasks using fluent one-line methods

    master

    The TaskService provides shorthand methods to perform common task creation actions in a single line of code.

    // Run a program every day on the local machine
    TaskService.Instance.AddTask("Test", QuickTriggerType.Daily, "myprogram.exe", "-a arg");
    
    // Run a custom COM handler on the last day of every month
    TaskService.Instance.AddTask("Test", new MonthlyTrigger { RunOnLastDayOfMonth = true }, 
        new ComHandlerAction(new Guid("{CE7D4428-8A77-4c5d-8A13-5CAB5D1EC734}")));
  7. Create a complex task using TaskDefinition

    master

    For full control, use the TaskService to create a TaskDefinition, configure its triggers and actions, and then register it with the RootFolder.

    using System;
    using Microsoft.Win32.TaskScheduler;
    
    class Program
    {
       static void Main(string[] args)
       {
          // Get the service on the remote machine
          using (TaskService ts = new TaskService(@"\\RemoteServer", "username", "domain", "password"))
          {
             // 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.
             // (Use the username here to ensure remote registration works.)
             ts.RootFolder.RegisterTaskDefinition(@"Test", td, TaskCreation.CreateOrUpdate, "username");
          }
       }
    }
  8. Quickly add a task using TaskService.Instance

    master

    For simple task creation, you can use the singleton TaskService.Instance to perform common operations in a single line of code. This is useful for running programs with basic triggers like daily schedules or using custom COM handlers.

    // Run a program every day on the local machine
    TaskService.Instance.AddTask("Test", QuickTriggerType.Daily, "myprogram.exe", "-a arg");
    
    // Run a custom COM handler on the last day of every month
    TaskService.Instance.AddTask("Test", new MonthlyTrigger { RunOnLastDayOfMonth = true }, 
        new ComHandlerAction(new Guid("{CE7D4428-8A77-4c5d-8A13-5CAB5D1EC734}")));