简体   繁体   中英

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())
{
    ts.RootFolder.DeleteTask(StrtMessage.Trim());
}

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

This works:

using TaskService ts = new TaskService();
if (ts.GetTask(StrTaskName) != null)
{
    ts.RootFolder.DeleteTask(StrTaskName);
}

Use StrTaskName instead of StrtMessage.Trim()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM