I'm trying to write some code to automatically "configure" a bunch of scheduled tasks on a machine. The program will read an XML file then "configure" the run times etc. of the task based on the information in the XML file.
The idea should be that the program can be "run" multiple times on the machine and automatically "add/remove" any effective changes. The way I was wanting to do this was as follows:
All tasks will be created with some special "prefix". When the program runs, it "deletes" all tasks with that prefix as they can be assumed to have been run by the program with a different, early, configuration.
The program then "creates" the new tasks.
So my "configuration" program needs be able to
- Get a list of all existing scheduled tasks on the machine
- Delete tasks by name
- Create new tasks by name
It will be simplest if I can write the program in C#.
There appear to be two ways I can do this:
The C# program explicitly runs the "schtasks.exe" program with appropriate command line parameters. It "reads" and parses the output of schtasks.exe to get the list of existing tasks so that it can delete them. It then runs schtasks.exe again to create the new tasks.
Use the ITaskManager interface via a .NET COM wrapper to programmatically query and schedule the tasks.
I'd prefer to go with #2 but I have a suspicion that this is "older" functionality - what I can find on the web implies that task manager functionality has improved since Windows NT.
Is schtasks.exe simply a command line tool written on top of the ITaskManager COM interface? Or are there things that schtasks.exe can do that the ITaskManager interface does not?
Does the ITaskManager interface/COM component generate ".job" files in the Scheduled tasks folder?
I am having some success using this library: http://www.codeproject.com/KB/cs/tsnewlib.aspx
It appears to use the ITaskScheduler COM interface internally and exposes the functionality that I need.
Thanks Mattias for your quick answer too (upvoted).
The new Task Scheduler 2.0 was introduced in Windows Vista, and you use the ITaskService interface to access it. I believe Schtasks.exe is built upon that API.
The TS 1.0 ITaskScheduler APIs still work though, for backward compatibility. So which one to use depend on which operating system versions you need to target and if you need the new features of TS 2.0.