ShaneTheTech
ShaneTheTech

Reputation: 157

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;"

Upvotes: 4

Views: 10342

Answers (2)

Noob-o-tron 5000
Noob-o-tron 5000

Reputation: 165

To add to the answer by Dove (I don't have the rep to comment yet), you can also use the FindTask method for TaskScheduler, which allows you to neglect specifying the sub-folder with the name if you enable searching within sub-folders by passing "True" as the second parameter; for instance.

using ( TaskService sched = new TaskService() ) {
    var task = sched.FindTask( "UniqueTaskName", true );

    if ( task != null ) {
        ...
    }
}

Upvotes: 3

dove
dove

Reputation: 20674

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);

Upvotes: 4

Related Questions