Unable to update a task using Task Scheduler Wrapper

0

In windows I have created task scheduler with trigger "At system startup". Now I'm trying to update the taskDefinition using

task.RegisterChanges();

It's giving me this exception Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

The scheduler was created using Admin account and I'm using the same account to update the scheduler.

That's how I am coding to update the Action path.

            TaskService taskService = new TaskService();
            TaskFolder tasksFolder = taskService.GetFolder(taskFolderName);

            if (tasksFolder != null)
            {

                foreach (var task in tasksFolder.Tasks)
                {
                    try
                    {

                        TaskDefinition taskDefinition = task.Definition;

                        ExecAction execAction = (ExecAction)taskDefinition.Actions.FirstOrDefault(); 

                        if (task.Name.StartsWith(KioskOperation))
                        {
                            execAction.Path = wwwRootPath + KioskOperation + setupFile;
                            currentSchedularName = KioskOperation;
                        }
                        else if (task.Name.StartsWith(AngelFish))
                        {
                            execAction.Path = wwwRootPath + PdAngelFish + setupFile;
                            currentSchedularName = AngelFish;
                        }
                        else if (task.Name.StartsWith(SpotFish))
                        {
                            execAction.Path = wwwRootPath + PdSpotFish + setupFile;
                            currentSchedularName = SpotFish;
                        }

                        task.RegisterChanges();

                        //taskService.GetFolder(taskFolderName).RegisterTaskDefinition(currentSchedularName, taskDefinition);
                    }
                    catch (Exception ex)
                    {
                        Console.Writeline("TaskSchedular failed while modifiying Task: " + task.Name + " with exception: " + ex.ToString());
                    }
                }
            }
            else {
                Console.Writeline("TaskSchedular cannot find Tasks in TaskFolder: " + taskFolderName);
            }
c#
scheduled-tasks
taskscheduler

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0