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.
%SystemRoot%\system32\Tasks
?This works:
using TaskService ts = new TaskService();
if (ts.GetTask(StrTaskName) != null)
{
ts.RootFolder.DeleteTask(StrTaskName);
}
Use StrTaskName instead of StrtMessage.Trim()
User contributions licensed under CC BY-SA 3.0