33

Does anyone have any recommendations for a .NET c# wrapper for the (COM based) Task Scheduler 2.0 API ?

Couldnt find anything using Google.

(I am aware there are .net based schedulers such as Quartz.net, but I need the fully functioned GUI in the windows Scheduler)

Thanks,

Matt

Matt Randle
  • 1,885
  • 2
  • 17
  • 22

4 Answers4

35

See the following project on GitHub:

https://github.com/dahall/taskscheduler

Dave Markle
  • 95,573
  • 20
  • 147
  • 170
Peter
  • 1,595
  • 13
  • 18
  • Yea thanks. Thats exactly what I was looking for. Somehow missed it when I searched for it. – Matt Randle Oct 21 '10 at 08:13
  • +1 exactly what i needed, i also searched a bit around until i decided to make a search here before posting the same question as Matt – Jim Wolff Nov 24 '11 at 12:54
  • Exactly what I was looking for. Task Scheduler is painful and the command line (schtasks) looks even worse. This API is working great so far. Thank you! – Robin Mar 26 '13 at 21:43
  • After successfully installing assemblies through Nugget it does not recognize **TaskService** in code. What can be the problem ? – Shai Jul 20 '17 at 11:53
11

If you dont want to use any third party wrapper then below is the sample code to schedule task.Code works in Windows 7.

//create task service instance
ITaskService taskService = new TaskSchedulerClass();
taskService.Connect();
ITaskDefinition taskDefinition = taskService.NewTask(0);
taskDefinition.Settings.Enabled = true;
taskDefinition.Settings.Compatibility = _TASK_COMPATIBILITY.TASK_COMPATIBILITY_V2_1;

//create trigger for task creation.
ITriggerCollection _iTriggerCollection = taskDefinition.Triggers;
ITrigger _trigger = _iTriggerCollection.Create(_TASK_TRIGGER_TYPE2.TASK_TRIGGER_TIME);
_trigger.StartBoundary = DateTime.Now.AddSeconds(15).ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss");
_trigger.EndBoundary = DateTime.Now.AddMinutes(1).ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss");
_trigger.Enabled = true;

///get actions.
IActionCollection actions = taskDefinition.Actions;
_TASK_ACTION_TYPE actionType = _TASK_ACTION_TYPE.TASK_ACTION_EXEC;

//create new action
IAction action = actions.Create(actionType);
IExecAction execAction = action as IExecAction;
execAction.Path = @"C:\Windows\System32\notepad.exe";
ITaskFolder rootFolder = taskService.GetFolder(@"\");

//register task.
rootFolder.RegisterTaskDefinition("test", taskDefinition, 6, null, null, _TASK_LOGON_TYPE.TASK_LOGON_NONE, null);
Tjcool
  • 391
  • 1
  • 3
  • 14
  • 1
    From where did you get the `ITaskService`, `TaskSchedulerClass`, `ITaskDefinition`, `ITriggerCollection`, `ITrigger`, `IActionCollection`, `IAction`, `IExecAction`, and `ITaskFolder`? The assembly with those classes and interfaces is not readily visible in Visual Studio 2010. – Zarepheth Jun 23 '14 at 22:49
  • 4
    Nevermind - I found the "taskschd.dll" file and after letting Visual Studio generate an Interop wrapped, was able to import it into my project. If only I could do the same with the PowerShell ISE and have it deploy to the destination target machines... – Zarepheth Jun 23 '14 at 22:59
  • You saved my day, thanks a lot! – Mikolaytis Mar 04 '18 at 10:24
5

Is the command line good enough?

at 23:39 /every:wednesday defrag c:

Rob Fonseca-Ensor
  • 15,510
  • 44
  • 57
0

You can get fully functioned GUI through following Nuget command.

PM> Install-Package TaskSchedulerEditor

Pabitra Dash
  • 1,461
  • 2
  • 21
  • 28