How to delete schedule task


I have created Schedule Task in Windows 2008 Server using below C# Code.It works fine but when I Delete Task from C# Code.Its shows an error message as "Access is Denied. (Exception from HRESULT : 0X80070005 (E_ACCESSDENIED) "

Please check below code and advise how to do that.. Creating Task :- (It works fine)

private void CreateTask(string StrTaskName,string  StrDate)
         using (TaskService ts = new TaskService())
             TaskDefinition td = ts.NewTask();
             td.RegistrationInfo.Description = "SMS Alert System";
             td.Principal.LogonType = TaskLogonType.InteractiveToken;

             TimeTrigger dt = (TimeTrigger)td.Triggers.Add(new   TimeTrigger());
             dt.StartBoundary = Convert.ToDateTime(StrDate);

             string doubleQuotedPath = string.Format(@"""{0}""",    StrTaskName);
             td.Actions.Add(new ExecAction(@"D:\Alert\SMSAlertSystem.exe", doubleQuotedPath, null));
             ts.RootFolder.RegisterTaskDefinition(StrTaskName, td);


Deleting Task :- (Not working)

using (TaskService ts = new TaskService())
asked on Stack Overflow Feb 7, 2018 by Muhammed Ismail • edited Feb 7, 2018 by Rand Random

2 Answers


You have probably wrong permission on process.

  • Have your process privileges to read/write in directory %SystemRoot%\system32\Tasks?
  • If not worked try to restart:
    • Task scheduler service
    • Windows
answered on Stack Overflow Feb 7, 2018 by Hajk Hovhannisyan • edited Feb 7, 2018 by Hajk Hovhannisyan

This works:

using TaskService ts = new TaskService();
if (ts.GetTask(StrTaskName) != null)

Use StrTaskName instead of StrtMessage.Trim()

answered on Stack Overflow May 28, 2021 by Michael Santos

User contributions licensed under CC BY-SA 3.0