Is there an option in possibly SCHTASKS to define where a scheduled job is actually created?
Instead of calling the
SCHTASKS I would recommend to use the .Net wrapper around the COM class
TaskScheduler, throught which you can interact with the Windows Task Scheduler. It required a bit more code but offers a rich set of properties and good control of the tasks. Doing so can use
ITaskFolder to create folders for your task(s). I include som of the code I use below (which will create a task called "MyTaskName" in the folder "MyTaskFolder". Also check out this article for good information on the subject.
TaskScheduler.TaskScheduler scheduler = new TaskScheduler.TaskScheduler(); scheduler.Connect(null, null, null, null); //run as current user. ITaskDefinition taskDef = scheduler.NewTask(0); taskDef.RegistrationInfo.Author = task.TaskAuthor; ... ITimeTrigger trigger = (ITimeTrigger)taskDef.Triggers.Create(_TASK_TRIGGER_TYPE2.TASK_TRIGGER_TIME); ... IExecAction action = (IExecAction)taskDef.Actions.Create(_TASK_ACTION_TYPE.TASK_ACTION_EXEC); ... ITaskFolder root = scheduler.GetFolder("\\"); root.CreateFolder("MyTaskFolder"); //// here IRegisteredTask regTask = root.RegisterTaskDefinition( "MyTaskName", taskDef, (int)_TASK_CREATION.TASK_CREATE_OR_UPDATE, null, // user null, // password _TASK_LOGON_TYPE.TASK_LOGON_INTERACTIVE_TOKEN, //User must already be logged on. The task will be run only in an existing interactive session. "" //SDDL );
Note that this will throw an exception if the folder already exists. Ypu can get the available folders using
ITaskFolderCollection folders = root.GetFolders(0);