Search code examples
c#.netstringtaskscheduled-tasks

Scheduled Task using Task Scheduler Managed Wrapper


I have done a lot of searching over the last few days in regards to Checking if a scheduled task exists, if so <insert awesome here>. Basically I have an app that installs and uninstalls our software's scheduled tasks. What I need to do now is have a checkbox be checked if the task is there and unchecked if it's not. There was a reference to using:

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

which isn't working for me, it's stating that the ScheduledTasks namespace could not be found. I believe I have what I need installed. "using Microsoft.Win32.TaskScheduler;"


Solution

  • I haven't seen this ScheduledTasks within this wrapper.

    The TaskScheduler Managed Wrapper uses a service idiom and you need to have the context of a folder.

    They have good examples in their documentation, including one for enumerating all tasks.

    If you want to find a particular task:

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

    if your tasks are within a folder, then use something like the following

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