Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete schedule task

Tags:

c#

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())
{
    ts.RootFolder.DeleteTask(StrtMessage.Trim());
}
like image 341
Muhammed Ismail Avatar asked Jun 30 '26 04:06

Muhammed Ismail


1 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
like image 119
Hajk Hovhannisyan Avatar answered Jul 01 '26 17:07

Hajk Hovhannisyan