Delete Task HRESULT: 0x80070005 (E_ACCESSDENIED))

Topics: Errors
May 18, 2016 at 7:58 AM

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);
ExecAction action = new ExecAction("notepad.exe", "c:\\test.log", null);
action.Id = "aaaaaaa";
TaskFolder tf = ts.RootFolder;
var folderList = tf.SubFolders;
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 2: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")