169

I am 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.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Boardy
  • 35,417
  • 104
  • 256
  • 447
  • 2
    Every you need is here: http://msdn.microsoft.com/en-us/library/aa383614(v=vs.85).aspx. API, examples and explanations on how to achieve what you need programmatically. – kroonwijk Sep 12 '11 at 22:52

2 Answers2

251

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
         ts.RootFolder.DeleteTask("Test");
      }
   }
}

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

Daniel Williams
  • 8,912
  • 15
  • 68
  • 107
Dmitry
  • 17,078
  • 2
  • 44
  • 70
  • 3
    Yes, you need to download and reference Microsoft.Win32.TaskScheduler.dll. The link is in the answer. – Dmitry Sep 13 '11 at 21:17
  • Yea sorry I thought I did add the reference but for some reason it wasn't. Sorry about that does work great though. Thanks for your help – Boardy Sep 13 '11 at 21:22
  • 1
    @Dmitry how do you start a task? Do you need to register it with the windows scheduler or something? – Haroon Jun 18 '13 at 08:56
  • 1
    This works a lot better than trying to create it by invoking a command line process – BradleyDotNET Feb 27 '14 at 20:17
  • Is there some way to update the created task like accessing by Task name something.? – Habeeb Oct 16 '15 at 06:24
  • @Dmitry Sorry about the Necrobump, how would I register a one-off task? – TechnicalTophat Jun 08 '16 at 09:20
  • @RhysO You could register the task as normal and as the final action call `schtasks.exe` to remove the task – Bassie Jul 05 '16 at 08:56
  • 3
    I see the reference is for win32, what if I my server is 64bit? – Seichi Nov 22 '16 at 21:26
  • It is possible to create recurring task with this library? – Ewerton Jun 13 '17 at 12:49
  • 5
    Since CodePlex is shutting down in a few months, please take note of the NuGet page for the Task Scheduler Managed Wrapper, at https://www.nuget.org/packages/TaskScheduler/. – David A. Gray Jun 28 '17 at 16:29
  • Also, for #Ewerton, I don't see why not, since it can be done in the underlying native libraries, or by way of XML documents and schtasks.exe. For #Bassie, you can register the task as a one-off event. By default, tasks are deleted after their last scheduled run, although there exists a flag to override this behavior, and retain the task. – David A. Gray Jun 28 '17 at 16:33
  • I ran this code, not as administrator. The task was successfully created. I got a System.UnauthorizedAccessException on the task deletion. Is there any other setup necessary to delete a task? Must it always be done by an administrator? – Matt May 08 '18 at 20:56
  • Bizarrely, if I change the name from "Test" to something 9 characters or longer, I no longer get this exception. – Matt May 09 '18 at 13:26
  • Is it possible to call a URL with this library ? I have seen the action types, it supports for exe and mail only. – Vivek Oct 04 '18 at 12:09
  • @Vivek You could just do chrome.exe [url] (or firefox or iexplore etc.) – PRMan Dec 03 '18 at 22:58
  • how to specify a specific time to run the task??, code is working very good – ABDULLAH MAKKI Aug 14 '20 at 17:54
41

This works for me https://www.nuget.org/packages/ASquare.WindowsTaskScheduler/

It is nicely designed Fluent API.

//This will create Daily trigger to run every 10 minutes for a duration of 18 hours
SchedulerResponse response = WindowTaskScheduler
    .Configure()
    .CreateTask("TaskName", "C:\\Test.bat")
    .RunDaily()
    .RunEveryXMinutes(10)
    .RunDurationFor(new TimeSpan(18, 0, 0))
    .SetStartDate(new DateTime(2015, 8, 8))
    .SetStartTime(new TimeSpan(8, 0, 0))
    .Execute();
Massimiliano Kraus
  • 3,638
  • 5
  • 27
  • 47
Uday Reddy
  • 411
  • 4
  • 2