Scheduled task using Task Scheduler Managed wrapper

Over the past few days, I have searched a lot regarding checking for the availability of a scheduled task, if so <insert awesome here> . Basically, I have an application that installs and removes our scheduled tasks. Now I need to check the checkbox if the task is there, and not checked if it is not. There was a link to use:

  ScheduledTasks st = new ScheduledTasks(server); string[] taskNames = st.GetTaskNames(); List<string> jobs = new List<string>(taskNames); 

which doesn't work for me, it claims that the ScheduledTasks namespace was not found. I believe that I have what I need to install. "using Microsoft.Win32.TaskScheduler;"

+4
source share
2 answers

I have not seen this ScheduledTasks in this shell.

TaskScheduler Managed Wrapper uses a utility idiom and you need to have a folder context.

They have good examples in the documentation , including for listing all tasks .

If you want to find a specific task:

 var t = taskService.GetTask(scheduledTaskName); bool taskExists = t!=null; if(taskExists) DoYourThing(); 

if your tasks are inside the folder then use something like the following

 var t = taskService.GetTask(taskFolder + "\\" + scheduledTaskName); 
+4
source

To add Dove to the answer (I have no comments yet), you can also use the FindTask method for TaskScheduler , which allows you to ignore specifying a subfolder with a name if you enable search in subfolders by passing "True" as the second parameter; eg.

 using ( TaskService sched = new TaskService() ) { var task = sched.FindTask( "UniqueTaskName", true ); if ( task != null ) { ... } } 
+3
source

All Articles