Tuesday, October 28, 2014

C# Creating a windows scheduled task inside a folder



Recently I needed to work with windows scheduled task, and noticed that it is possible to consolidate/organize the tasks inside tasks scheduler in folders. Essentially a possibility to consolidate the tasks by topics or vendor.


In order to work with task scheduler I’m using the Task Scheduler Managed Wrapper, by David Hall, available from taskscheduler.codeplex.com or as a nuget download.

Sub Folder management:

So it took me a minute to understand that sub folder collection and all the related functions are located under RootFolder and not available as immediate TaskService children.

TaskService taskService = new TaskService();
 if (taskService.RootFolder.SubFolders.FirstOrDefault(x=>x.Name=="MyFolder")==null)

taskService.RootFolder.SubFolders["MyFolder"].RegisterTaskDefinition("MyTask", taskDefinition);

You need to check for existing folder with the same name, calling CreateFolder with existing name will throw an exception.

No comments:

Post a Comment