This project has moved. For the latest updates, please go here.

Delete Task HRESULT: 0x80070005 (E_ACCESSDENIED))

Topics: Errors
May 18, 2016 at 6:58 AM
Hello,

I wrote a small program to create and delete a task. Task creation works but when I try to delete the created task I get the error E_ACCESSDENIED. The code is:
TaskService ts = new TaskService();
TaskDefinition td = ts.NewTask();
td.RegistrationInfo.Description = "Task Test";
DailyTrigger dt = new DailyTrigger();
dt.Repetition.Interval = TimeSpan.FromMinutes(1);
 
td.Triggers.Add(dt);
ExecAction action = new ExecAction("notepad.exe", "c:\\test.log", null);
action.Id = "aaaaaaa";
td.Actions.Add(action);
 
TaskFolder tf = ts.RootFolder;
 
var folderList = tf.SubFolders;
 
tf.CreateFolder("\\MyFolder");
 
TaskFolder sub = tf.SubFolders["MyFolder"];
           
var user = Path.Combine(Environment.UserDomainName, Environment.UserName);
var a = sub.RegisterTaskDefinition(_taskName, td, TaskCreation.CreateOrUpdate, user);
 
sub.DeleteTask(_taskName, false);
Any ideas why can I not delete the task?
Thank you very much for your help.
May 19, 2016 at 1:56 AM
I run your code and everything looks OK~
You can try to run your program as administrators(Right click .exe file of your program and click "Run as administrator")