无法使用任务计划程序包装程序更新任务

时间:2020-10-09 06:16:36

标签: c# scheduled-tasks taskscheduler

在Windows中,我已经创建了带有“系统启动时”触发器的任务计划程序。现在,我正在尝试使用

更新taskDefinition
task.RegisterChanges();

这给了我这个例外,拒绝访问。 (来自HRESULT的异常:0x80070005(E_ACCESSDENIED))

计划程序是使用Admin帐户创建的,而我使用的是同一帐户来更新计划程序。

这就是我编写更新操作路径的方式。

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

0 个答案:

没有答案